Home > Community > Forums > PCB SKILL > human ordering sorting challenge for skill

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 human ordering sorting challenge for skill 

Last post Mon, Jun 30 2014 9:51 PM by Ejlersen. 4 replies.
Started by Ejlersen 19 Jun 2014 02:29 AM. Topic has 4 replies and 1148 views
Page 1 of 1 (5 items)
Sort Posts:
  • Thu, Jun 19 2014 2:29 AM

    • Ejlersen
    • Top 10 Contributor
    • Joined on Mon, Jul 28 2008
    • Aalborg, Copenhagen
    • Posts 569
    • Points 9,965
    human ordering sorting challenge for skill Reply

    Hi 

    I've numerous times come across the issue of sorting strings in a more "human way of thinking fashion" 

    I've played around with the different sorting predicates like 'lessp, 'alphaNumCmp, 'axlStrcmpAlpNum but none are really sorting in the way I would need. I had hoped the 'axlStrcmpAlpNum would do what I wanted, but mixed strings are a challenge.

    Example (end sorting result that I would like)

    "22" 

    "111234" 

    "111237"

    "111a32"

    "112323" 

    "11235a" 

    "11235f"

    "abc211"

    "xyz211"

    I've come across some LISP code for this, but must admit that although I've done a lot of skill programming the code at the link below are a bit "black magic like" for me, it does not translate directly into a skill program

    http://rottcodd.wordpress.com/2007/12/15/human-order-sorting/  

    So maybe someone could shed som light on how to transform this into something usefull in Skill.

    Hint:  http://www.davekoelle.com/alphanum.html has a number of other language implementations for the same type of sorting, it actually explain and illustrate the problem very nicely.

    Thank you very much in advance

    Best regards

    Ole 

    Best regards Ole
    • Post Points: 20
  • Sun, Jun 22 2014 4:39 PM

    • eDave
    • Top 10 Contributor
    • Joined on Sun, Jul 13 2008
    • Christchurch, 00-NZ
    • Posts 736
    • Points 15,930
    Re: human ordering sorting challenge for skill Reply

    Based on the Perl example I came up with the following code. However it (and the Perl) gives a different result to your requirement:

    defun( HumanCmp (str1, str2)

     prog((a, b)

    ;split strings into chunks

    a = Chunkify(str1)

    b = Chunkify(str2)

     

    ;while we have chunks to compare.

    while(a && b

    when(alphaNumCmp(car(a), car(b), t) < 0, return(t)); return t if b > a

    a = cdr(a), b = cdr(b)

    )

    return(nil)

    ))

     

    ; split on character type transitions

    defun( Chunkify (str)

     let((chunk, chunks, prevCh)

    foreach(ch, parseString(str, "")

    cond(

    (!prevCh, chunk = ch)

    (type(readstring(ch)) != type(readstring(prevCh)), chunks = cons(chunk, chunks), chunk = ch)

    (t, chunk = strcat(chunk, ch))

    )

    prevCh = ch

    )

    when(chunk, reverse(cons(chunk, chunks)))

    ))

    ; sort('("11235a" "11235f" "111a32" "abc211" "xyz211" "22" "111234" "111237" "112323"), 'HumanCmp)

    ; Ole wants: ("22" "111234" "111237" "111a32" "112323" "11235a" "11235f" "abc211" "xyz211")

    ; I get:     ("22" "111a32" "11235a" "11235f" "111234" "111237" "112323" "abc211" "xyz211")

    Dave Elder, Tait Communications
    • Post Points: 20
  • Sun, Jun 22 2014 11:50 PM

    • Ejlersen
    • Top 10 Contributor
    • Joined on Mon, Jul 28 2008
    • Aalborg, Copenhagen
    • Posts 569
    • Points 9,965
    Re: human ordering sorting challenge for skill Reply

    Hi Dave

    Thank you very much, I can see your point about sort

    Not sure how I'll fix that but thanks for your code, the first challenge that I did not really see how to solve was the chunkify function, but that works nicely, so now I need to figure out how to do a sort that acts they way I need it to work :-)

    Best regards

    Ole 

    Best regards Ole
    • Post Points: 20
  • Mon, Jun 30 2014 2:52 PM

    • odanoy
    • Not Ranked
    • Joined on Mon, Nov 3 2008
    • Austin, TX
    • Posts 14
    • Points 190
    Re: human ordering sorting challenge for skill Reply
    Ole,
     
    Using Dave HumanCmp as a starting point how about comparing each character one at a time but also compare the length of each strings first? Of course this is going to be really slow for large list since each string must be split into single characters. And I'm sure there must a better way to convert numbers into strings but I couldn't find anything better than using sprintf. 
     
    defun( HumanCmp (str1, str2)
     prog((a, b)
    ;split strings into single characters
    ;prepend length of string so strings are sorted first by their lengths
    a = cons(sprintf(nil "%d" strlen(str1)), parseString(str1, ""))
    b = cons(sprintf(nil "%d" strlen(str2)), parseString(str2, ""))
    ;while we have chunks to compare.
    while(a && b
    when(alphaNumCmp(car(a), car(b), t) < 0, return(t)); return t if b > a
    when(alphaNumCmp(car(a), car(b), t) > 0, return(nil)); return nil if b < a
    a = cdr(a), b = cdr(b)
    )
    return(nil)
    ))
     
    ; sort('("11235a" "11235f" "111a32" "abc211" "xyz211" "22" "111234" "111237" "112323"), 'HumanCmp) 
    ; ("22" "111234" "111237" "111a32" "112323" "11235a" "11235f" "abc211" "xyz211") 
     
    Olivier 
    • Post Points: 20
  • Mon, Jun 30 2014 9:51 PM

    • Ejlersen
    • Top 10 Contributor
    • Joined on Mon, Jul 28 2008
    • Aalborg, Copenhagen
    • Posts 569
    • Points 9,965
    Re: human ordering sorting challenge for skill Reply

    Hi Olivier

    Thank you for your input on this, I got some code from a long time friend that I've been able to convert into something that can be used.

    I'll try your method to figure out if that works better for me. I'll however need to check for length since any length text must be after numbers, so "aa" will be after any string starting with a number.

    I'm in the same boat as you with numbers to strings, only aware of sprintf. 

    Best regards

    Ole 

    Best regards Ole
    • Post Points: 5
Page 1 of 1 (5 items)
Sort Posts:
Started by Ejlersen at 19 Jun 2014 02:29 AM. Topic has 4 replies.