[Question]
  [
The code should take input a text (not mandatory can be anything file, stdin, string for JavaScript, etc):
```
This is a text and a number: 31.
```
The output should contain the words with their number of occurrence, sorted by the number of occurrences in descending order:
```
a:2
and:1
is:1
number:1
This:1
text:1
31:1
```
Notice that 31 is a word, so a word is anything alpha-numeric, number are not acting as separators so for example `0xAF` qualifies as a word. Separators will be anything that is not alpha-numeric including `.`(dot) and `-`(hyphen) thus `i.e.` or `pick-me-up` would result in 2 respectively 3 words. Should be case sensitive, `This` and `this` would be two different words, `'` would also be separator so `wouldn`and `t` will be 2 different words from `wouldn't`. 
Write the shortest code in your language of choice.
Shortest correct answer so far:
* **[grep and coreutils - 42 bytes](https://codegolf.stackexchange.com/a/19572/12328)**
  
[Answer]
## grep and coreutils ~~44~~ 42
```
grep -io '[a-z0-9]*'|sort|uniq -c|sort -nr
```
Test:
```
printf "This is a text and a number: 31." |
grep -io '[a-z0-9]*'|sort|uniq -c|sort -nr
```
Results in:
```
  2 a
  1 This
  1 text
  1 number
  1 is
  1 and
  1 31
```
### Update
* Use case-insensitive option and shorter regex. Thanks Tomas.
[Answer]
# Java 8: 289
Which is pretty good, since java is a very non-golfy language.
```
import java.util.stream.*;class C{static void main(String[]a){Stream.of(a).flatMap(s->of(s.split("[\\W_]+"))).collect(Collectors.groupingBy(x->x,Collectors.counting())).entrySet().stream().sorted(x,y->x.getValue()-y.getValue()).forEach(e->System.out.println(e.getKey()+":"+e.getValue()));}
```
Ungolfed:
```
import java.util.stream.*;
class C {
    static void main(String [] args){
        Stream.of(args).flatMap(arg->Stream.of(arg.split("[\\W_]+")))
            .collect(Collectors.groupingBy(word->word,Collectors.counting()))
            .entrySet().stream().sorted(x,y->x.getValue()-y.getValue())
            .forEach(entry->System.out.println(entry.getKey()+":"+entry.getValue()));
    }
}
```
Run from the command line:
```
java -jar wordCounter.jar This is a text and a number: 31.
```
[Answer]
## APL (57)
```
⎕ML←3⋄G[⍒,1↓⍉G←⊃∪↓Z,⍪+⌿∘.≡⍨Z←I⊂⍨(I←⍞)∊⎕D,⎕A,⎕UCS 96+⍳26;]
```
e.g.
```
      ⎕ML←3⋄G[⍒,1↓⍉G←⊃∪↓Z,⍪+⌿∘.≡⍨Z←I⊂⍨(I←⍞)∊⎕D,⎕A,⎕UCS 96+⍳26;]
This is a text and a number: 31.
 a       2
 This    1
 is      1
 text    1
 and     1
 number  1
 31      1
```
Explanation:
* `⎕D,⎕A,⎕UCS 96+⍳26`: numbers, uppercase letters, lowercase letters
* `(I←⍞)∊`: read input, store in `I`, see which ones are alphanumeric
* `Z←I⊂⍨`: split `I` in groups of alphanumeric characters, store in `Z`
* `+⌿∘.≡⍨Z`: for each element in `Z`, see how often it occurs
* `Z,⍪`: match each element in `Z` pairwise with how many times it occurs
* `G←⊃∪↓`: select only the unique pairs, store in `G`
* `⍒,1↓⍉G`: get sorted indices for the occurrences
* `G[`...`;]`: reorder the lines of `G` by the given indices
[Answer]
# C#: ~~153c~~ ~~144c~~ ~~142c~~ ~~111c~~ ~~115c~~ ~~118c~~ ~~114c~~ 113c
*(via LINQPad in "C# Statements" mode, not including input string)*
## Version 1: 142c
```
var s = "This is a text and a number: 31."; // <- line not included in count
s.Split(s.Where(c=>!Char.IsLetterOrDigit(c)).ToArray(),(StringSplitOptions)1).GroupBy(x=>x,(k,e)=>new{s,c=e.Count()}).OrderBy(x=>-x.c).Dump();
```
Ungolfed:
```
var s = "This is a text and a number: 31.";
s.Split(                                                     // split string on multiple separators
    s.Where(c => !Char.IsLetterOrDigit(c))                   // get list of non-alphanumeric characters in string
     .ToArray(),                                             // (would love to get rid of this but needed to match the correct Split signature)
    (StringSplitOptions)1                                    // integer equivalent of StringSplitOptions.RemoveEmptyEntries
).GroupBy(x => x, (k, e) => new{ s = k, c = e.Count() })     // count by word
 .OrderBy(x => -x.c)                                         // order ascending by negative count (i.e. OrderByDescending)
 .Dump();                                                    // output to LINQPad results panel
```
Results:
![Results](https://i.stack.imgur.com/V2MPI.png)
## Version 2: 114c
(`[\w]` includes `_`, which is incorrect!; `[A-z]` includes `[ \ ] ^ _ ``; settling on `[^_\W]+`)
```
var s = "This is a text and a number: 31."; // <- line not included in count
Regex.Matches(s, @"[^_\W]+").Cast<Match>().GroupBy(m=>m.Value,(m,e)=>new{m,c=e.Count()}).OrderBy(g=>-g.c).Dump();
```
Ungolfed:
```
Regex.Matches(s, @"[^_\W]+")                                   // get all matches for one-or-more alphanumeric characters
     .Cast<Match>()                                            // why weren't .NET 1 collections retrofitted with IEnumerable<T>??
     .GroupBy(m => m.Value, (m,e) => new{ m, c = e.Count() })  // count by word
     .OrderBy(g => -g.c)                                       // order ascending by negative count (i.e. OrderByDescending)
     .Dump();                                                  // output to LINQPad results panel
```
Results:
(as Version 1)
[Answer]
### R, 58 char
```
sort(table(unlist(strsplit(scan(,""),"[[:punct:]]"))),d=T)
```
Usage:
```
sort(table(unlist(strsplit(scan(,""),"[[:punct:]]"))),d=T)
1: This is a text and a number: 31.
9: 
Read 8 items
     a     31    and     is number   text   This 
     2      1      1      1      1      1      1 
```
[Answer]
## perl6: 49 characters
```
.say for get.comb(/\w+/).Bag.pairs.sort(-*.value)
```
Comb input for stuff matching `\w+`, put resulting list of words in a `Bag`, ask for their pairs and sort them by negative value. (The `*` is a [Whatever](http://perlcabal.org/syn/S02.html#The_Whatever_Object) star, it's not multiplication here)
output:
```
"a" => 2
"This" => 1
"is" => 1
"text" => 1
"and" => 1
"number" => 1
"31" => 1
```
[Answer]
# Python ~~101~~ 97
```
import re
a=re.split('[_\W]+',input())
f=a.count
for w in sorted(set(a),key=f)[::-1]:print w,f(w)
```
Now works with newline:
```
$ python countword.py <<< '"This is    a text and a number: 31, and a\nnewline"'
a 3
and 2
31 1
number 1
newline 1
is 1
text 1
This 1
```
[Answer]
## PHP - 84 bytes
```
<?$a=array_count_values(preg_split('/[_\W]+/',$argv[1],0,1));arsort($a);print_r($a);
```
Input is accepted as a command line argument, e.g.:
```
$ php count-words.php "This is a text and a number: 31."
```
Output for the sample string:
```
Array
(
    [a] => 2
    [number] => 1
    [31] => 1
    [and] => 1
    [text] => 1
    [is] => 1
    [This] => 1
)
```
[Answer]
**PowerShell (40)**
```
$s -split"\W+"|group -ca|sort count -des
```
$s is a variable that contains the input string.
[Answer]
# GNU awk + coreutils: 71 69
```
gawk 'BEGIN{RS="\\W+"}{c[$0]++}END{for(w in c)print c[w],w}'|sort -nr
```
Although `gawk asort` works on associative arrays, it apparently does not preserve the index values, necessitating the external `sort`
```
printf "This is a text and a number: 31." | 
gawk 'BEGIN{RS="\\W+"}{c[$0]++}END{for(w in c)print c[w],w}'|sort -nr
2 a
1 This
1 text
1 number
1 is
1 and
1 31
```
# GNU awk 4.x: 100 93
A slightly larger but pure gawk solution using `PROCINFO` to set the default sort order for the associative array (appears to require a relatively recent gawk - > 4.x?)
```
BEGIN{RS="\\W+";PROCINFO["sorted_in"]="@val_num_desc"}
{c[$0]++}
END{for(w in c)print c[w],w}
```
[Answer]
## Perl 69
```
$h{$_}++for<>=~/\w+/g;print"$_: $h{$_}
"for sort{$h{$b}-$h{$a}}keys%h
```
Added recommendations from @primo and @protist
[Answer]
## Powershell: ~~57~~ ~~55~~ ~~53~~ ~~62~~ 57
*(not including input string)*
```
$s = "This is a text and a number: 31."    # <-- not counting this line...
[Regex]::Matches($s,"[^_\W]+")|group -ca|sort{-$_.Count}
```
returns:
```
Count Name                      Group
----- ----                      -----
    2 a                         {a, a}
    1 and                       {and}
    1 31                        {31}
    1 number                    {number}
    1 This                      {This}
    1 is                        {is}
    1 text                      {text}
```
(with props to @microbian for group -ca)
[Answer]
# EcmaScript 6
## Version 1 (108 characters)
```
s.split(_=/[^a-z\d]/i).map(x=>_[x]=-~_[x]);keys(_).sort((a,b)=>_[a]<_[b]).map(x=>x&&console.log(x+':'+_[x]))
```
## Version 2 (102 characters)
```
s.split(_=/[^a-z\d]/i).map(x=>_[x]=-~_[x]);keys(_).sort((a,b)=>_[a]<_[b]).map(x=>x&&alert(x+':'+_[x]))
```
## Version 3 (105 characters)
```
s.match(_=/\w+/g).map(x=>_[x]=-~_[x]);alert(keys(_).sort((a,b)=>_[a]<_[b]).map(x=>x+':'+_[x]).join('\n'))
```
## Version 4 (94 characters)
```
s.match(_=/\w+/g).map(x=>_[x]=-~_[x]);keys(_).sort((a,b)=>_[a]<_[b]).map(x=>alert(x+':'+_[x]))
```
## Version 5 (without alert; 87 characters)
```
s.match(_=/\w+/g).map(x=>_[x]=-~_[x]);keys(_).sort((a,b)=>_[a]<_[b]).map(x=>x+':'+_[x])
```
## Version 6 (100 characters)
```
keys(_,s.match(_=/\w+/g).map(x=>_[x]=-~_[x])).sort((a,b)=>_[a]<_[b]).map(x=>console.log(x+':'+_[x]))
```
---
## Output:
```
a:2
31:1
This:1
is:1
text:1
and:1
number:1
```
[Answer]
# Groovy 77 82
changed regex from `[^\w]+` to `[^\d\p{L}]+` in order to solve problem with underscore
```
String s = 'This is a text and a number: 31'
def a=s.split(/[^\d\p{L}]+/) 
a.collectEntries{[it, a.count(it)]}.sort{-it.value}
```
without first line, 82 characters
output:
```
[a:2, This:1, is:1, text:1, and:1, number:1, 31:1]
```
[Answer]
# Javascript - ~~132~~ 126 chars !
### (Shortest JS code)
```
o={},a=[]
for(i in s=s.split(/[\W_]+/))o[z=s[i]]=o[z]+1||1
for(j in o)a.push([j,o[j]])
a.sort(function(b,c){return c[1]-b[1]})
```
Improved the regex and some edits.
---
## Ungolfed
```
s = s.split(/[\W_]+/), o={}, a=[]; // split along non-char letters, declare object and array
for (i in s) { n = s[i]; o[n] = o[n] + 1 || 1 } // go through each char and store it's occurence
for (j in o) a.push( [j, o[j]] ); // store in array for sorting
a.sort(function (b, c){ return c[1] - b[1]; }); // sort !
```
---
> 
> <= // make s = "How shiny is this day is isn't is"
> 
> 
> => 
>  [ [ 'is', 3 ],  
> 
>  [ 'How', 1 ],  
> 
>  [ 'shiny', 1 ],  
> 
>  [ 'this', 1 ],  
> 
>  [ 'day', 1 ],  
> 
>  [ 'isn', 1 ],  
> 
>  [ 't', 1 ] ]
> 
> 
> 
---
### Old - ~~156~~ ~~143~~ ~~141~~ ~~140~~ 132 chars
```
s=s.split(/[^\w]+/g),o={}
for(i in s){n=s[i];o[n]=o[n]+1||1}a=[]
for(j in o)a.push([j,o[j]])
a.sort(function(b,c){return c[1]-b[1]})
```
---
Gave a first try at golfing. Feedback appreciated.
[Answer]
## EcmaScript 6, 115 100 87 (without prompt&alert)
Thanks to @eithedog:
```
s.match(/\w+/g,a={}).map(w=>a[w]=-~a[w]),keys(a).map(w=>[w,a[w]]).sort((a,b)=>b[1]-a[1])
```
With prompt and alert (100):
```
prompt(a={}).match(/\w+/g).map(w=>a[w]=-~a[w]);alert(keys(a).map(w=>[w,a[w]]).sort((a,b)=>b[1]-a[1]))
```
Run it in Firefox.
[Answer]
# Ruby ~~58~~ ~~82~~ 65
```
h=Hash.new 0
gets.scan(/[\d\w]+/){h[$&]+=1}
p *h.sort_by{|k,v|-v}
```
Test run:
```
$ ruby counttext.rb <<< "This is a text and a number: 31."
["a", 2]
["text", 1]
["This", 1]
["is", 1]
["and", 1]
["number", 1]
["31", 1]
```
---
Edit 58->80: Ok, I was way off. I forgot to sort the words by occurrences. Also, `Array#uniq` is not an enumerator, but uses a given block to compare elements, so passing `puts` to it didn't filter out duplicates (not that it says that we should).
[Answer]
**F# - 169**
```
let f s=(s+"").Split(set s-set(['a'..'z']@['A'..'Z']@['0'..'9'])|>Set.toArray)|>Seq.where((<>)"")|>Seq.countBy id|>Seq.sortBy((~-)<<snd)|>Seq.iter((<||)(printfn"%s:%d"))
```
Degolfed: 
```
let count (s : string) =
    s.Split (set s - set (['a'..'z']@['A'..'Z']@['0'..'9']) |> Set.toArray)
 |> Seq.where ((<>) "")
 |> Seq.countBy id
 |> Seq.sortBy ((~-) << snd)
 |> Seq.iter ((<||) (printfn "%s:%d"))
```
Output when called from fsi:
```
> "This is a text and a number: 31." |> f
a:2
This:1
is:1
text:1
and:1
number:1
31:1
val it : unit = ()
```
**Update:** Some explanation as requested in the comments.
Uses set functions to generate an array of non alphanumeric characters in the input to pass to String.Split, then uses sequence functions to filter out empty strings, generate word counts and print the result.
Some golfing tricks: Adds an empty string to the function argument s to force type inference of the argument as a string rather than explicitly declaring the type. Uses Seq.where rather than Seq.filter to save a few characters (they are synonyms). Mixes forward pipe and ordinary function application in an attempt to minimize characters. Uses currying and (op) syntax to treat <> ~- and <|| operators as regular functions to avoid declaring lambdas to filter empty strings, sort by descending count and print tuples.
[Answer]
## Python - 95 ( now ***87*** thanks to @primo)
```
d=__import__('re').findall(r'\w+',raw_input())
print sorted(map(lambda y:(y,d.count(y)),d))
```
Sample input :
```
'This is a text and a number: 31'
```
Sample output :
```
[('This', 1),('is', 1), ('a', 2),('text', 1),('and', 1),('a', 2),('number', 1),('31', 1)]
```
Any improvement sugestion would be appreciated
[Answer]
# JavaScript 160 144 (Edited: to meet requirements)
```
f=Function;o={};s.replace(/\w+/g,f('a','o[a]=++o[a]||1'));Object.keys(o).sort(f('b,c','return o[c]-o[b]')).map(f('k','console.log(k+" "+o[k])'))
```
Unminified:
```
f=Function;
o = {};
s.replace(/\w+/g, f('a','o[a]=++o[a]||1'));
Object.keys(o).sort(f('b,c', 'return o[c]-o[b]')).map(f('k','console.log(k+" "+o[k])'))
```
Logs each word to console in order, passing the following string:
`s="This is sam}}ple text 31to test the effectiveness of this code, you can clearly see that this is working-as-intended, but you didn't doubt it did you?.";`
Outputs:
```
you 3
this 2
is 2
can 1
text 1
31to 1
test 1
the 1
effectiveness 1
of 1
This 1
code 1
sam 1
ple 1
clearly 1
see 1
that 1
working 1
as 1
intended 1
but 1
didn 1
t 1
doubt 1
it 1
did 1 
```
I don't have the heart to use `alert()`.
[Answer]
# k [71 chars]
```
f:{s:" ",x;`_k!m@k:|(!m)@<.:m:#:'=`$1_'(&~((),/:s)like"[a-zA-Z0-9]")_s}
```
Any other character except alphanumeric chars will be considered as delimiter.
### example
```
f "This is a text and a number: 31."
a     | 2
31    | 1
number| 1
and   | 1
text  | 1
is    | 1
This  | 1
```
### example
```
f "won't won won-won"
won| 4
t  | 1
```
[Answer]
# Javascript (135)
```
u=/\w+/g
for(i=s.length;i--;)for(w in a=s.match(u))u[w=a[w]]=u[w]||a.reduce(function(p,c){return p+=w==c},0)==i&&!console.log(w+":"+i)
```
Unminified:
```
u=/\w+/g;for (i=s.length;i--;)
    for(w in a=s.match(u))
        u[w=a[w]] = u[w] || 
           a.reduce(function(p,c){return p+=w==c},0)==i && !console.log(w+":"+i)
```
Loops over every possible number of matches in descending order, and outputs words with that number of occurrences. Just to be horrible.
Notes: Alert would have reduced the length some. Strictly speaking alphanumeric should be `[^\W_]`
[Answer]
# Haskell (153 = 104 code + 49 import)
Pretty straight-forward, totally composed function... no argument even necessary! This is my first golf, so go easy, maybe? :)
```
import Data.Char
import Data.List
import Data.Ord
so=reverse.(sortBy$comparing snd).(map(\t@(x:_)->(x,length t))).group.sort.(map$filter isAlphaNum).words
```
Output:
```
*Main> so "This is a text and a number: 31."
[("a",2),("text",1),("number",1),("is",1),("and",1),("This",1),("31",1)]
```
[Answer]
# q (50)
```
desc count each group" "vs ssr[;"[^0-9A-Za-z]";" "]
```
* ssr replaces non alphanumeric
* " "vs splits the result into a symbol list
* count each group counts creates a dict matching distinct elements of the list with the number of occurances
* desc sorts the dict by descending values
edit: fixed accidentally matching ascii 58-64 and 91-96
[Answer]
# Pure Bash (no external programs), 164
This is longer than I'd hoped, but I wanted to see if the necessary counting and sorting (in the right direction) could be done purely with `bash` arrays (associative and non-associative):
```
declare -A c
for w in ${@//[[:punct:]]/ };{ ((c[$w]++));}
for w in ${!c[@]};{ i=${c[$w]};((m=i>m?i:m));s[$i]+=$w:;}
for((i=m;i>0;i--));{ printf "${s[i]//:/:$i
}";}
```
Save as a script file, `chmod +x`, and run:
```
$ ./countoccur This is a text and a number: 31.
a:2
and:1
number:1
text:1
31:1
is:1
This:1
$ 
```
[Answer]
# AWK
```
awk -vRS='[^A-Za-z0-9]' '$0{c[$0]++}END{for(i in c)print c[i]"\t"i": "c[i]|"sort -nr|cut -f2-"}'
```
Does the job without gawkish extensions:
```
$ echo 'This is a text and a number: 31.' | awk -vRS='[^A-Za-z0-9]' '$0{c[$0]++}END{for(i in c)print c[i]"\t"i": "c[i]|"sort -nr|cut -f2-"}'
a: 2
This: 1
text: 1
number: 1
is: 1
and: 1
31: 1
```
If printing "count: word" instead, it would be a bit shorter but I wanted to mimic the given example output...
[Answer]
# [Tcl](http://tcl.tk/), 99 bytes
```
proc C s {lmap w [split [regsub -all \[^\\w|\ \] $s {}]] {dict inc D $w}
lsort -s 2 -inde 1 -de $D}
```
[Try it online!](https://tio.run/##HU3NCsMgGLv3KULp1UG3267tI@ymDqyVTbBW9CsOOp/dySAkOeSHtKs1xF1jQsLpNhWQwVNwlsCjeaVjAVPOQfCnEPkrICSGFi1S4lytJlivMWPIpXNpjwSWcAWzfjUYwRoPc6n/5Vbr@sfbJjQokPkQlF@b9ce2mHjHbbz0XcEZDkrtRoBPTWSpPw "Tcl – Try It Online")
[Answer]
Python 2.X (108 - Characters)
```
print'\n'.join('{}:{}'.format(a,b)for a,b in __import__("collections").Counter(raw_input().split()).items())
```
Python 3.X (106 - Characters)
```
print('\n'.join('{}:{}'.format(a,b)for a,b in __import__("collections").Counter(input().split()).items())
```
[Answer]
# Haskell - 137
```
import Data.List
count text=let textS=(words(text\\".-\':")) in (sortBy (\(_,n) (_,m) -> compare m n)).nub$map(\t->(t,(length.(filter(==t)))textS)) textS
```
[Answer]
# Python 3 - 76
The requirement of splitting on non-alphanumeric chars unfortunately extends the code by 19 chars. The output of the following is shown correctly. If you are not sure, add a `.most_common()` after the `.Counter(...)`.
```
i=__import__
print(i('collections').Counter(i('re').findall('\w+',input())))
```
### In/Output
Given the input of `This is a text and a number: 31.` you get following output:
```
Counter({'a': 2, 'is': 1, 'This': 1, 'and': 1, '31': 1, 'number': 1, 'text': 1})
```
I tried it with other values like 
```
1 2 3 4 5 6 7 8 2 1 5 3 4 6 8 1 3 2 4 6 1 2 8 4 3 1 3 2 5 6 5 4  2 2 4 2 1 3 6
```
to ensure, the output-order does not rely on the key's value/hash. This example produces:
```
Counter({'2': 8, '3': 6, '1': 6, '4': 6, '6': 5, '5': 4, '8': 3, '7': 1})
```
But as I said, `print(i('collections').Counter(i('re').findall('\w+',input())).most_common())` would return the results as an *definitly* ordered list of tuples.
---
## Python 3 - 57 *(if a space would be enough for splitting :P)*
```
print(__import__('collections').Counter(input().split()))
```
]