sort with list

This topic contains 7 replies, has 4 voices, and was last updated by  medi 2 years, 1 month ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #7502

    medi
    Participant

    I have used ‘Compare’ below to sort a linked list in BlitzMax:

    How to do this in Monkey 2?

    #7503

    degac
    Participant

    Hi

    to place source code use the button ‘crayon’ in the text-box, it will open a proper ‘code editor’ where you can paste the source.

    About list and ‘compare’ method follow this example

    http://monkey2.monkey-x.com/forums/topic/a-clever-way-to-filter-lists/

    #7504

    medi
    Participant

    Thanks for the help!

    How about this:

    in the manual, rather than a kind of IComapre? Will play with that for a while, then I will go back to the link.

    #7505

    Mark Sibly
    Keymaster

    Here’s the basic idea:

    The ‘lambda’ is an ‘inline’ funtion. You can also use a global function or method here, eg: stack.Sort( MyGlobalCompareFunc ).

    The ‘<=>’ operator might look a bit weird, but it basically just returns an int value <0 if x<y, >0 if x>y and 0 if x=y. This is the default behavior if you just call Sort() with no comparison param.

    #7506

    impixi
    Participant

    Mark just beat me to it, about the ‘<=>’ operator. Anyway, here’s what I whipped up:

    [/crayon]
    #7507

    medi
    Participant

    Thanks, so back to my sort and making life easier, I got this:

    Which prints:

    Next stage?

    a csvReader which I should figure out without readLine()

    #7520

    Mark Sibly
    Keymaster

    …actually, this:

    per_list.Sort(ComparePersons(x, y) )

    looks a bit wrong. This actually calls the ‘ComparePersons’ function, but you probably wanted to pass the function to Sort() instead. The way to do that is simply:

    per_list.Sort( ComparePersons )

    Easy!

    Since there is no ‘()’ after ComparePersons, the function is *not* actually called…

    When you go:

    per_list.Sort( ComparePersons(x,y) )

    …this does actually call ComparePersons (which returns an int) and then passes the returned int as a parameter to Sort(), which is probably not what you meant.

    Basically, ‘func()’ calls a function, while ‘func’ on its own does not. This takes a bit of getting used to but the main idea here is that it allows you to pass functions around just like they were ints or floats or arrays or strings or whatever!

    #7524

    medi
    Participant

    Thanks Mark, at least I tried 😉

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.