7

Hi I am absolute beginner of node.js Today I tried the following code

var fs, arr;
var dir, str;
var cont, item;

fs=require('fs');
cont=fs.readFileSync('unsort.txt').toString();
arr=cont.split('\n');
arr.sort();

for(str=arr.shift();str&&(item=arr.shift());)
    str+='\n'+item;
fs.writeFileSync('sort_by_script.txt', str);

the above node.js code reads a file as string, from the directory of the node.exe. Splits the string by newline ('\n') to get a array. Sorts the array and prints the sorted array to the file. So as a whole the script reads a file sort the entries and saves the sorted entry in another file. The problem is the sorted order is not correct. I tried sorting the content of unsort.txt manually using MS Excel by which I got the correct order of sort. Can any one help me why the arr.sort() is not working correct. You can download unsort.txt, sort_by_script.txt, sort_by_ms_excel.txt and node.exe in the package [Sort.rar][1]

Note : unsort.txt has no numbers. All are only alphabets.

Examples from unsort.txt:

appjs
gbi
node
frame
require
process
module
WebSocket
webkitAudioContext
webkitRTCPeerConnection
webkitPeerConnection00
webkitMediaStream
MediaController
HTMLSourceElement
TimeRanges
5
  • sort by what? Are you aware that you sort strings and not numbers? Commented Apr 4, 2013 at 7:00
  • can you describe the content of the file and how you want to sort it? Please post it here, the download wont work forever (and is not working at the moment) Commented Apr 4, 2013 at 7:01
  • unsort.txt @ ilovec.in/Sort/unsort.txt Commented Apr 4, 2013 at 7:35
  • sort_by_script.txt @ ilovec.in/Sort/sort_by_script.txt Commented Apr 4, 2013 at 7:36
  • sort_by_ms_excel.txt @ ilovec.in/Sort/sort_by_ms_excel.txt Commented Apr 4, 2013 at 7:36

2 Answers 2

12

If you do not pass a custom search function the sort function sorts lexically, numbers get cast to strings and so it happens that e.g. "10" is before "3". So the strings get sorted.

You can pass a custom function to the sort function which decides the order of the items, in case of numbers this would be an example (Be careful as numbers in your example would be strings if you dont cast / parse them to numbers) :

var numsort = function (a, b) {
    return a - b;
}

var numbers = new Array(20, 2, 11, 4, 1);

var result = numbers.sort(numsort);

Another example for strings:

var sortstring = function (a, b)    {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if (a < b) return 1;
    if (a > b) return -1;
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

but unsort.txt has no number at all. All are only alphabets.
please post the contents here. it was a guess and an example, your google folder cant be opened and it wont be there forever (in opposite to this question)
you can try to download now its in a different link
post them here, it is easy to do even if it is only an extract of the file
the file has 564 line of entries. If I post it here it would make a mess. I think the download should be working now. Try again.
|
4

I would use

arr.sort((obj1, obj2) => {
            return obj1.localeCompare(obj2);
        });

That will most likely solve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.