Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Windows sorting in javascripts

    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.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    maybe:
    Code:
    yourArray.sort(function (a,b){return a[0]==b[0]?0:(a[0]<b[0]?-1:1)});
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It works but not works..

    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,
    Code:
    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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by DevaV View Post
    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.

    PHP Code:
    ['#@!$%.doc''test1.doc''test2.doc''1.doc'].sort() 
    will result in the array

    PHP Code:
    ['#@!$%.doc''1.doc''test1.doc''test2.doc'
    Secondly, Javascript uses the Unicode table 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:

    Quote Originally Posted by Kor View Post
    maybe:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •