fbpx

Sorting a list containing objects

  • Jean-Pierre Maertens
  • Jean-Pierre Maertens's Avatar Topic Author
  • Offline
More
1 year 10 months ago #1 by Jean-Pierre Maertens
Sorting a list containing objects was created by Jean-Pierre Maertens
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#.

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago - 1 year 10 months ago #2 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Sorting a list containing objects
Hi Jean Pierre,

I've now tried with LinQ (Phil will be pleased <g>):
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
Last edit: 1 year 10 months ago by Wolfgang Riedmann. Reason: better formatting

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago - 1 year 10 months ago #3 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Sorting a list containing objects
This should be the simpliest:
oTrackList := oTrackList:orderby( {|o| o:Start } ):ToList()

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

LinQ starts to be sympatic to me....
Last edit: 1 year 10 months ago by Wolfgang Riedmann.

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago - 1 year 10 months ago #4 by Karl Faller
Replied by Karl Faller on topic Sorting a list containing objects
thx for asking, had no idea ;)- ah, Wolfgang was faster and used another way...
Now:
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
Last edit: 1 year 10 months ago by Karl Faller. Reason: typo and additonal info

Please Log in or Create an account to join the conversation.

  • Jean-Pierre Maertens
  • Jean-Pierre Maertens's Avatar Topic Author
  • Offline
More
1 year 10 months ago #5 by Jean-Pierre Maertens
Replied by Jean-Pierre Maertens on topic Sorting a list containing objects
Karl & Wolfgang,

Thank you very much !!

JP

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #6 by Otto Christiaanse
Replied by Otto Christiaanse on topic 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.

Wolfgang Riedmann wrote:

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


Regards,
Otto

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #7 by Phil Hepburn
Replied by Phil Hepburn on topic 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.

Please Log in or Create an account to join the conversation.

  • Jean-Pierre Maertens
  • Jean-Pierre Maertens's Avatar Topic Author
  • Offline
More
1 year 10 months ago #8 by Jean-Pierre Maertens
Replied by Jean-Pierre Maertens on topic 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?)

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #9 by Otto Christiaanse
Replied by Otto Christiaanse on topic Sorting a list containing objects

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.

Please Log in or Create an account to join the conversation.

More
1 year 10 months ago #10 by Phil Hepburn
Replied by Phil Hepburn on topic 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.

Please Log in or Create an account to join the conversation.