fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Sorting a list containing objects

Sorting a list containing objects 2 years 2 weeks ago #1

  • jpmaertens
  • jpmaertens's Avatar Topic Author
  • Offline
  • Posts: 14
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.

Sorting a list containing objects 2 years 2 weeks ago #2

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Last edit: by wriedmann. Reason: better formatting

Sorting a list containing objects 2 years 2 weeks ago #3

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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....
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Last edit: by wriedmann.

Sorting a list containing objects 2 years 2 weeks ago #4

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

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

Last edit: by FFF. Reason: typo and additonal info

Sorting a list containing objects 2 years 2 weeks ago #5

  • jpmaertens
  • jpmaertens's Avatar Topic Author
  • Offline
  • Posts: 14
Karl & Wolfgang,

Thank you very much !!

JP

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

Sorting a list containing objects 2 years 1 week ago #6

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.

Sorting a list containing objects 2 years 1 week ago #7

  • Phil Hepburn
  • Phil Hepburn's Avatar
  • Offline
  • Posts: 737
  • Karma: 7
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.

Sorting a list containing objects 2 years 1 week ago #8

  • jpmaertens
  • jpmaertens's Avatar Topic Author
  • Offline
  • Posts: 14
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.

Sorting a list containing objects 2 years 1 week ago #9

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.

Sorting a list containing objects 2 years 1 week ago #10

  • Phil Hepburn
  • Phil Hepburn's Avatar
  • Offline
  • Posts: 737
  • Karma: 7
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.

  • Page:
  • 1