SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with custom sorting

    Our site has many documents whose titles are indexed in this format:

    1-1, 1-2, 1-2A, etc., 1-11, 1-12, 2-1, 2-2, 2-3A, 2-10, etc.

    (The actual titles follow the format "Procedure 1-5 Smoking in the Workplace")

    I've been trying to devise a custom sort in VBscript that will treat, say, 2-10 as numerically greater than 2-1. This would also mean that 10-1 would have to be greater than 2-1. I'm not necessarily looking for code examples, but mostly for sorting techniques to solve this problem.

  2. #2
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorting these type of alphanumerics usually requires padding with zeroes - sorting - then stripping out the padding before display.

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or break out the numeric portions as arrays of numeric (not text) element references and sort that way?
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I looked around for ways to insert the zeros, but this seemed to involve trying to convert the numbers on both sides of the dash to integers, and it choked on instances such as 1-2A.

    I just did some quick research and apparently VBscript has limited support for regular expressions, something I didn't realize. I'll see if this yields better results.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a tentative solution - I used VBScript's RegExp function to find the index number and then separate the index into three pieces. These I store as array elements for relatively easy comparison.

    A shame that VBScript doesn't have support for associative arrays. I tried the Dictionary object a while back as a substitute, but with no luck.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    359
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just a thought, perhaps you want to re-visit your db design. It appears the two halves of your data are two different things, maybe they should be in different columns in the data.
    Doug G
    =====
    "If you ain't the lead dog, the view is always the same - Anon

  7. #7
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Carstonio View Post
    A shame that VBScript doesn't have support for associative arrays. I tried the Dictionary object a while back as a substitute, but with no luck.
    VBScript does use the dictionary object in lieu of associative arrays and it should work.

    You can also use Javascript arrays since ASP supports other languages. You can even use them multiple languages on the same page.

    A VBscript function can be called from Javascript and vice versa so there are many options to choose from for your solution.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Doug G View Post
    Just a thought, perhaps you want to re-visit your db design. It appears the two halves of your data are two different things, maybe they should be in different columns in the data.
    There's no database involved. The script simply reads the filenames in the directory. The old version used a bubble sort for alphabetical order, except that the indexing system didn't match alphanumeric sorting.

    Strange that on the Windows server I'm using, I can open up the directory in a window, click the Name bar to sort the files, and they will be the order that I want. It's as if Windows and VBScript's strcomp function use different rules for alphanumeric sorting. (And I tried both binary and textual comparison for the latter.)

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,269
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Is the Windows OS using the juliet filenames (8 characters max)?

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Is the Windows OS using the juliet filenames (8 characters max)?
    The filenames appear to be much longer than that. Is there a setting on the server that I could test in ASP? I don't have admin rights to IIS on the server.

  11. #11
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    359
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's no database involved. The script simply reads the filenames in the directory. The old version used a bubble sort for alphabetical order, except that the indexing system didn't match alphanumeric sorting
    Oh, sorry, I should learn to read better!

    Another thought, I haven't used the filesystem object in years but it offers the ability to traverse through directory/file names. Perhaps using it to list the filenames would take advantage of the proper sorting done by windows explorer? Just a total guess.
    Doug G
    =====
    "If you ain't the lead dog, the view is always the same - Anon


Bookmarks

Posting Permissions

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