Sorting a list containing objects

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Anonymous

Sorting a list containing objects

Post 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#.
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Sorting a list containing objects

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Sorting a list containing objects

Post 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....
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Sorting a list containing objects

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
jpmaertens

Sorting a list containing objects

Post by jpmaertens »

Karl & Wolfgang,

Thank you very much !!

JP
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

Sorting a list containing objects

Post 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
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Sorting a list containing objects

Post 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.
jpmaertens

Sorting a list containing objects

Post 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?)
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

Sorting a list containing objects

Post 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.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Sorting a list containing objects

Post 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.
Post Reply