Results 1 to 6 of 6

Thread: Once again, sorting pb

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    527

    Once again, sorting pb

    Hi all.

    I've searched the past threads about sorting and even though there are many existing resolved threads, none address my problem. So let me explain:

    Hi need to sort strings in an array. Nothing to fancy yet. But my problem is that some of the values I need to sort are actually numbers! For example, I might have to sort:

    12
    67
    2
    X67
    L101
    etc...

    When using standard string sorting, "2" is always greater than "19" since the comparison is made on the first character.

    So does anyone knows a string sorting algorythm where "2" will not be greater than "19"?

    Thanks!
    Don't ask why, just reboot!

  2. #2
    Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    37
    Never done exactly what you are doing. However, you can format the strings so that comparisons are more accurate:

    Format(strArray(lngCtr), "000000")

    this will allow numbers to sort accurately:

    000002
    000019
    etc...

    Good luck
    If at first you don't succeed...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    527

    ya well....

    Thanks but that's pretty much what I want to avoid.

    You see to be able to do that, I'd need to browse all existing values to figure which one has the largest number of characters and then browse once again all values to add "0" in front of other values so that they would match the length of that longest value. Then I'd be able to apply the sort. Then browse all values again to take off the extra "0" in front of the values.....

    Provided I have to read and write these values in an Excel file (slow process!!!), I'm looking for a way around that.

    But thanks anyways for suggesting!
    Don't ask why, just reboot!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    You say that SOME of the strings have numeric values. Thus I assume that some do not have numeric values.

    Do the ones that have numeric value all go above or below the ones that lack numeric values?

    If this is the case, it is probably best to split the list into two lists, one with numbers, and one without. Sort both lists, and put them back together.

    If there is a function that evaluates a string to a number for sorting, I would expect it to be a fairly slow operation, so this solution might be faster.

    If the numeric values begin all strings such as "1Park", "35mph", etc., then the problem is more difficult.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    527

    Well...

    I'm afraid the problem is more difficult...

    Most values will be numeric, but I may get values with only letters.I will also get lots of mixed ones where sometimes it may be "letters" followed by "numbers" or vice versa... I don't really care which one will come first, as long as its is consistent.

    A pretty big mess...

    I guess I'm far from resolving that problem...

    Thanks for answering!
    Don't ask why, just reboot!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I suspect there will not be a simple solution to this, but I have a few further suggestions to make.

    If I recall, the standard library quick sort function in C++ takes a pointer to something that tells the function how to compare one to another. You may have to do something like that. I would think this would require you to write a custom sort function (that's easy, there are any number of references to those, look for quicksort or shellsort).

    In every sort function that I am aware of, there is a statement of the type a>b. This is where the function determines where a value belongs relative to another value. You will want to replace that statement with a function call. The function will have to take two string arguments and return with either a boolean (Saying: "Yes, argument 1 is greater than argument 2"), or a value (Saying:"This is the larger (or smaller) between argument 1 and argument 2"). Which you choose depends on what the function does with the data, but something like ShellSort is so simple in code that the decision should be fairly obvious.

    The function will have to do the comparisons, but once this functionality has been isolated, you can probably come up with a fairly quick way to do this. Here are a few suggestions:

    Do you care about capitalization? If not, you could use StrConv() to convert to all upper or lower case.

    Take each argument and turn it into something else. If the argument IsNumeric(), turn it into a number. If it starts with a number, split it into a number and a string.

    In any event, once all you need to do is write a function that determines whether A is greater than B, it should be fairly straightforward for you to figure out how to determine that.

Posting Permissions

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



Click Here to Expand Forum to Full Width