About Monkey 2 › Forums › Monkey 2 Programming Help › List sorting function help
This topic contains 5 replies, has 3 voices, and was last updated by
Mark Sibly
1 year, 10 months ago.
-
AuthorPosts
-
June 5, 2017 at 7:54 pm #8476
I need to sort a list containing a custom class.
The class contains lots of stuff, but I want to sort it by name.
If the name begins with @ it should be at the very top and + after that.
(Or would be happen automatically when sorting text?)How would I go about doing that in Monkey 2?
June 5, 2017 at 8:36 pm #8478The basic idea is…
Monkey1234list.Sort( Lambda( x:C,y:C )Return x.Name<=>y.NameEnd )The lambda (or method/function) needs to return the same result as the ‘spaceship’ operator, ie: an int value <0 if x<y; an int value >0 if x>y; or 0 if x=y.
June 6, 2017 at 1:58 am #8482It has to be Lambda:Int though, right? (confused)
But with that method, the + sign in names would cause users to jump to the top.
I want names that starts with @ to be first, second should be names that starts with +.
And all the rest of the names below that.
That bit confuses me…Right now, I’ve just done so that when comparing names, the @ and + are replaced with 1 and 2, only during the sort test.
June 6, 2017 at 3:18 am #8483You could maybe do something like this:
[/crayon]Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586[crayon-5cba87dadadde493258395 inline="true" ]#Import "<std>"Using std..Function Main()Local recList := New RecordList()recList.Add(New Record("Dude"))recList.Add(New Record("Jane Doe"))recList.Add(New Record("@Dude"))recList.Add(New Record("+Jane Doe"))recList.Add(New Record("+Dude"))recList.Add(New Record("@Jane Doe"))Print "~nUnsorted~n-----------"recList.PrintAll()recList.Sort(RecordList.SortByName)Print "~nSorted (First pass) ~n-----------"recList.PrintAll()recList.Sort(RecordList.SortByCharTweak)Print "~nSorted (Second pass) ~n-----------"recList.PrintAll()EndClass RecordList Extends List<Record>PublicMethod New()Super.New()EndMethod PrintAll()For Local rec := Eachin SelfPrint rec.ToString()NextEndFunction SortByName:Int(lhs:Record, rhs:Record)Return lhs.Name <=> rhs.NameEndFunction SortByCharTweak:Int(lhs:Record, rhs:Record)If (lhs.Name.Length <= 0) Or (rhs.Name.Length <= 0) Then Return 0If lhs.Name[0] = "@"[0] Then Return lhs.Name>rhs.NameIf rhs.Name[0] = "@"[0] Then Return rhs.Name>lhs.NameIf lhs.Name[0] = "+"[0] Then Return lhs.Name>rhs.NameIf rhs.Name[0] = "+"[0] Then Return rhs.Name>lhs.NameReturn 0EndEndClass RecordPublicMethod New(name:String)Name = nameEndProperty Name:String()Return _nameSetter (value:String)_name = valueEndMethod ToString:String()Return NameEndPrivateField _name:StringEndJune 6, 2017 at 7:53 am #8487You’ll need to write a custom comparison routine, something like (untested)…
Monkey12345678910Function CompareNames:Int( lhs:String,rhs:String )if lhs.StartsWith( "@" ) and rhs.StartsWith( "@" ) return lhs<=>rhsif lhs.StartsWith( "@" ) return -1if rhs.StartsWith( "@" ) return +1if lhs.StartsWith( "+" ) and rhs.StartsWith( "+" ) return lhs<=>rhsif lhs.StartsWith( "+" ) return -1if rhs.StartsWith( "+" ) return +1Return lhs<=>rhsEndJune 6, 2017 at 7:53 am #8488It has to be Lambda:Int though, right? (confused)
Oops, yes.
-
AuthorPosts
You must be logged in to reply to this topic.