440,189 Members | 1,559 Online
Need help? Post your question and get tips & solutions from a community of 440,189 IT Pros & Developers. It's quick & easy.

# Array sort function sorts on chars not numbers ... help ! how to sort numbers

 P: n/a My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? Thanks, em Jul 23 '05 #1
5 Replies

 P: n/a "GIMME" wrote in message news:3f**************************@posting.google.c om... My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? Thanks, em Here's a solution; watch for word-wrap. sortnum.htm Jul 23 '05 #2

 P: n/a "GIMME" wrote My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? Two steps. See inserted lines: HTH Ivo Jul 23 '05 #3

 P: n/a GIMME said:My question is ... How do I sort an Array on numeric, not character values ? It's hard to believe that you could find any documentation on Array.sort() that doesn't include an example of how to sort numerically. There's one at this link: http://tinyurl.com/4ohf4 Jul 23 '05 #4

 P: n/a JRS: In article <40*********************@news.wanadoo.nl>, dated Thu, 22 Jul 2004 02:44:28, seen in news:comp.lang.javascript, Ivo posted :"GIMME" wrote My question is ... How do I sort an Array on numeric, not character values That might be FAQ-worthy; I've seen it quite a few times. There's obvious generalisation to other types of sort, e.g. dates using getTime or valueOf, both of which should be quick. 2. And add:function sortnumeric(a,b){ return parseFloat(a)-parseFloat(b); } That requires, for each of the ~ o(N ln N) comparisons, two conversions of number to string and two conversions of string to number. Only subtraction is needed. I can believe that adding a large constant to each number could be quicker, for large sorts, since it avoids calling a user-defined function. Someone tell ECMA to add a numericSort method to anything that has a sort method. The chief benefit would be that, eventually, it would appear in documentation, help files, and books; and it should be a little quicker. -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. © Web - w. FAQish topics, links, acronyms PAS EXE etc : - see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc. Jul 23 '05 #5

 P: n/a GIMME wrote: My question is ... How do I sort an Array on numeric, not character values ? function numSort(a, b) { return a - b; } var a = new Array(...); a.sort(numSort); It is described in detail in the Client-side JavaScript 1.3+ References, if you would have cared to RTFFAQ/RTFM prior to posting you would have known. PointedEars Jul 23 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.