473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

<html>
<head>
<script>
function TestSort() {
var nums = new Array(0);
nums[0] = 1;
nums[1] = 2;
nums[2] = 10;
nums[3] = 3;
nums.sort();
for ( i = 0 ; i < 4 ; i++ ) {
alert(nums[i]);
}
return true;
}
</script>
</head>
<body bgcolor="lightblue">
<input type="button" onclick="TestSort();" value="Click Me">
</body>
</html>
Jul 23 '05 #1
5 1640
"GIMME" <gi*******************@yahoo.com> 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

<html>
<head>
<script>
function TestSort() {
var nums = new Array(0);
nums[0] = 1;
nums[1] = 2;
nums[2] = 10;
nums[3] = 3;
nums.sort();
for ( i = 0 ; i < 4 ; i++ ) {
alert(nums[i]);
}
return true;
}
</script>
</head>
<body bgcolor="lightblue">
<input type="button" onclick="TestSort();" value="Click Me">
</body>
</html>


Here's a solution; watch for word-wrap.
<html>
<head>
<title>sortnum.htm</title>
<script type="text/javascript">
function TestSort() {
var nums = new Array(0);
nums[0] = 1;
nums[1] = 2;
nums[2] = 10;
nums[3] = 3;
nums.sort();
for (var i=0; i<nums.length; i++) {
nums[i] = (10000 + nums[i]);
}
nums.sort();
var numx = "";
for (var j=0; j<nums.length; j++) {
numx += (nums[j] - 10000) + "\n";
}
alert(numx);
}
</script>
</head>
<body onload="TestSort()">
</body>
</html>

Jul 23 '05 #2
Ivo
"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:
<html>
<head>
<script>
function TestSort() {
var nums = new Array(0);
nums[0] = 1;
nums[1] = 2;
nums[2] = 10;
nums[3] = 3;
nums.sort();
1. Make that:
nums.sort(sortnumeric);
for ( i = 0 ; i < 4 ; i++ ) {
alert(nums[i]);
}
return true;
}
2. And add:
function sortnumeric(a,b){ return parseFloat(a)-parseFloat(b); }
</script>
</head>
<body bgcolor="lightblue">
<input type="button" onclick="TestSort();" value="Click Me">
</body>
</html>


HTH
Ivo
Jul 23 '05 #3
Lee
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

<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1196882>

Jul 23 '05 #4
JRS: In article <40*********************@news.wanadoo.nl>, dated Thu,
22 Jul 2004 02:44:28, seen in news:comp.lang.javascript, Ivo
<no@thank.you> 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 <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
15
by: David | last post by:
sorry for the last post, itchy fingers. I'm having a bit of difficulty sorting images named in sequential numerical order. Here are the image names and how I need them sorted. image1.jpg...
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
2
by: almurph | last post by:
Folks, I have an array of chars nad would like to remove any repeating terms. Any ideas as how to do this? Thanks, Al.
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.