Sorting a list containing objects
Sorting a list containing objects
Hello !
I am stuck on the syntax to sort objects in a list.
var oTracklist := list<TrackObject>{}
... after filling the oTracklist
Each trackobject has a property "start", which is also an object that has a millisecond() method. I would like to sort the tracklist on this millisecond value, but I have no clue how to do this in x#.
I am stuck on the syntax to sort objects in a list.
var oTracklist := list<TrackObject>{}
... after filling the oTracklist
Each trackobject has a property "start", which is also an object that has a millisecond() method. I would like to sort the tracklist on this millisecond value, but I have no clue how to do this in x#.
Sorting a list containing objects
Hi Jean Pierre,
I've now tried with LinQ (Phil will be pleased <g>):
Wolfgang
I've now tried with LinQ (Phil will be pleased <g>):
Code: Select all
using System.Collections.Generic
using System.Linq
using System
function Start( ) as void
local oTrackList as List<Track>
oTrackList := List<Track>{}
oTrackList:Add( Track{ 12 } )
oTrackList:Add( Track{ 23 } )
oTrackList:Add( Track{ 34 } )
oTrackList:Add( Track{ 45 } )
oTrackList:Add( Track{ 56 } )
oTrackList:Add( Track{ 67 } )
foreach oTrack as Track in oTrackList
System.Console.WriteLine( "Track " + oTrack:Start:ToString() )
next
System.Console.WriteLine( "Sorted now..." )
var oSortedList := from oTrack as Track in oTrackList;
orderby oTrack:Start descending;
select oTrack
System.Console.WriteLine( "result object is of type " + oSortedList:GetType():Name )
foreach oTrack as Track in oSortedList
System.Console.WriteLine( "Track " + oTrack:Start:ToString() )
next
return
class Track
constructor( nStart as int )
self:Start := nStart
return
property Start as int auto
end class
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Sorting a list containing objects
This should be the simpliest:
or
LinQ starts to be sympatic to me....
Code: Select all
oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
Code: Select all
oTrackList := oTrackList:orderbyDescending( {|o| o:Start } ):ToList()
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Sorting a list containing objects
thx for asking, had no idea - ah, Wolfgang was faster and used another way...
Now:
If you want to reuse your sort, write Class myComparer with a CompareTo Method, and use this as param for the sort-call.
HTH
Now:
Code: Select all
USING System
USING System.Collections.Generic
FUNCTION Start( ) AS VOID
System.Console.WriteLine("Hello x#!")
VAR oList:= List<Customer>{}
VAR c1 := Customer{}
VAR c2 := Customer{}
c1:Name:="Karl"
c1:time:= 50
c2:Name:="Jean"
c2:time:= 10
oList:Add(c1)
oList:Add(c2)
oList:Sort()
FOREACH o AS Customer IN oList
? o:name, o:time
NEXT
RETURN
CLASS Customer IMPLEMENTS IComparable<Customer>
PUBLIC name AS STRING
PUBLIC time AS INT
METHOD CompareTo(cus AS Customer) AS INT
LOCAL nResult AS INT
IF SELF:time < cus:time
nResult:= -1
ELSEIF SELF:time > cus:time
nResult:= 1
ELSE
nResult:= 0
END
RETURN nResult
END CLASS
HTH
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Sorting a list containing objects
The .ToList() is not necessary. In this case: one starts with a List, then the result of the orderby is a list. And even in other cases the ToList is not always necessary.
Otto
Regards,Wolfgang Riedmann wrote:Code: Select all
oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
Otto
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Sorting a list containing objects
Hey guys !
Some of you are really getting the hang of this LINQ stuff - well done ;-0)
Nick will be proud of all of us, if he ever returns from South America.
Speak soon,
Phil.
Some of you are really getting the hang of this LINQ stuff - well done ;-0)
Nick will be proud of all of us, if he ever returns from South America.
Speak soon,
Phil.
Sorting a list containing objects
oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
works ok
oTrackList := oTrackList:orderby( {|o| o:Start } )
gives a compiler error
error XS0266: Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<ScriptPortal.Vegas.TrackEvent>' to 'System.Collections.Generic.List<ScriptPortal.Vegas.TrackEvent>'. An explicit conversion exists (are you missing a cast?)
works ok
oTrackList := oTrackList:orderby( {|o| o:Start } )
gives a compiler error
error XS0266: Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<ScriptPortal.Vegas.TrackEvent>' to 'System.Collections.Generic.List<ScriptPortal.Vegas.TrackEvent>'. An explicit conversion exists (are you missing a cast?)
Sorting a list containing objects
Sorry, my bad, in the case you really want a list, you have to use ToList().Jean-Pierre Maertens wrote:oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
works ok
oTrackList := oTrackList:orderby( {|o| o:Start } )
gives a compiler error
If you just want the result to be enumerable (and create a new variable to hold the result), you can skip the ToList().
For a foreach, you don't need a List, just something you can enumerate through.
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Sorting a list containing objects
Hi guys,
Having spent MANY hours with this LINQ stuff, all I can say is that for little 'stuff' like this you just have to try it out. 'Suck it and see'.
After a while you get a feeling for how to do it and get it to work for you ;-0)
At least you are getting some good and positive results. Must be good.
Oh! and then you will get into generic types so we use .ToList<Customer>() and similar things.
Keep on a-LINQing ;-0)
Cheers and good night!
Phil.
Having spent MANY hours with this LINQ stuff, all I can say is that for little 'stuff' like this you just have to try it out. 'Suck it and see'.
After a while you get a feeling for how to do it and get it to work for you ;-0)
At least you are getting some good and positive results. Must be good.
Oh! and then you will get into generic types so we use .ToList<Customer>() and similar things.
Keep on a-LINQing ;-0)
Cheers and good night!
Phil.