...

View Full Version : Windows sorting in javascripts



DevaV
09-09-2011, 10:36 AM
Hi,

I need javascript to sort as windows sorts its file. For eg: When the files name contains special characters, number and alphabets(#@!$%.doc, test1.doc,test2.doc, 1.doc) javascript sorts alphabets first and then special characters, but windows sorts special characters first and then alphabets in ascending. I want javascript to sort my array as windows.

Kindly help me out in this case.

Thanks,
Deva.

Kor
09-09-2011, 11:14 AM
maybe:


yourArray.sort(function (a,b){return a[0]==b[0]?0:(a[0]<b[0]?-1:1)});

DevaV
09-12-2011, 06:59 AM
Hi Thanks for your solution but when I tried with the above sorting condition but it sorts some what correctly for small files but when i use it for large amount of files it gives result in usual sorting order. The code that I used is,



for (var i =0; i<ct; i++)
sourceArray[i] = String(vaultList.columnValue(i,"Name"));

sourceArray.sort(function (a,b){return String(a)==String(b)? 0:(String(a)<String(b)?-1:1)});



Kindly give me the solution.

Thanks,
Deva

Kor
09-12-2011, 01:50 PM
String() is rather used as a constructor and I don't see which is its utility here. And really I don't understand how a code could work "for small files" and fail for "large amount of files". A code either works or it does not.

And, what do you mean by "files" in that context? JavaScript has nothing to do with files.

venegal
09-12-2011, 04:22 PM
Hi,

I need javascript to sort as windows sorts its file. For eg: When the files name contains special characters, number and alphabets(#@!$%.doc, test1.doc,test2.doc, 1.doc) javascript sorts alphabets first and then special characters, but windows sorts special characters first and then alphabets in ascending.

First of all, this is not true.


['#@!$%.doc', 'test1.doc', 'test2.doc', '1.doc'].sort()

will result in the array


['#@!$%.doc', '1.doc', 'test1.doc', 'test2.doc']

Secondly, Javascript uses the Unicode table (http://www.tamasoft.co.jp/en/general-info/unicode.html) for sorting purposes. If you want a different sorting order, you will have to specify the exact sorting rules you wish to use. 'Just like windows does it' isn't enough.

Thirdly, this isn't going to do any good:


maybe:


yourArray.sort(function (a,b){return a[0]==b[0]?0:(a[0]<b[0]?-1:1)});


All it does is use just the first character for sorting, instead of all of them, which isn't really helpful (and, what's worse, whatever happens to different strings starting with the same character depends entirely on the browser). It will still use the standard Unicode table, though, so it's the same as the standard sort, only worse.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum