Web Analytics Made Easy -
StatCounter Windows sorting in javascripts - CodingForum

Announcement

Collapse
No announcement yet.

Windows sorting in javascripts

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    maybe:
    Code:
    yourArray.sort(function (a,b){return a[0]==b[0]?0:(a[0]<b[0]?-1:1)});
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      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

      Comment


      • #4
        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
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          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:

          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.
          .My new Javascript tutorial site: http://reallifejs.com/
          .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
          .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎