xsharp.eu • Sorting a list containing objects
Page 1 of 1

Sorting a list containing objects

Posted: Thu Mar 09, 2017 3:07 pm
by Anonymous
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#.

Sorting a list containing objects

Posted: Thu Mar 09, 2017 4:50 pm
by wriedmann
Hi Jean Pierre,

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

Sorting a list containing objects

Posted: Thu Mar 09, 2017 5:01 pm
by wriedmann
This should be the simpliest:

Code: Select all

oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
or

Code: Select all

oTrackList := oTrackList:orderbyDescending( {|o| o:Start } ):ToList()
LinQ starts to be sympatic to me....

Sorting a list containing objects

Posted: Thu Mar 09, 2017 5:04 pm
by FFF
thx for asking, had no idea ;) - ah, Wolfgang was faster and used another way...
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
If you want to reuse your sort, write Class myComparer with a CompareTo Method, and use this as param for the sort-call.
HTH

Sorting a list containing objects

Posted: Thu Mar 09, 2017 5:27 pm
by jpmaertens
Karl & Wolfgang,

Thank you very much !!

JP

Sorting a list containing objects

Posted: Thu Mar 09, 2017 7:27 pm
by Otto
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.
Wolfgang Riedmann wrote:

Code: Select all

oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
Regards,
Otto

Sorting a list containing objects

Posted: Thu Mar 09, 2017 8:26 pm
by Phil Hepburn
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.

Sorting a list containing objects

Posted: Thu Mar 09, 2017 8:45 pm
by jpmaertens
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?)

Sorting a list containing objects

Posted: Thu Mar 09, 2017 9:59 pm
by Otto
Jean-Pierre Maertens wrote:oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()
works ok
oTrackList := oTrackList:orderby( {|o| o:Start } )
gives a compiler error
Sorry, my bad, in the case you really want a list, you have to use ToList().
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.

Sorting a list containing objects

Posted: Thu Mar 09, 2017 10:43 pm
by Phil Hepburn
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.