List sort ?

BlitzMax Forums/BlitzMax Programming/List sort ?

Hi :)

I'm trying to sort a list content but without result :

Global List:TList = CreateList()

Type mytype

	Field value:Int
	Field name:String
	
	Function create:mytype(val:Int)
		Local t:mytype = New mytype
		t.value = val
		t.name = "TOTO"+Rand(100,255)
		ListAddLast(List,t)
		Return t
	End Function
End Type

mytype.Create(12)
mytype.Create(6)
mytype.Create(28)
mytype.Create(128)

List.Sort(True)

For i:mytype = EachIn list
	Print i.value
Next


I have try to find method in the forum (search) but without succes...

You have to override the method compare (other:object) in your own type

it returns an int.

return 1 means other is > than self
return 0 means other is = to self
return -1 means other is < than self

Thanks dream, i'll try :)

I love you dream :) thanks for help :)

This is a good method to sort list value :

Global List:TList = CreateList()

Type mytype

	Field value:Int
	Field name:String
	
	Function create:mytype(val:Int)
		Local t:mytype = New mytype
		t.value = val
		t.name = "TOTO"+Rand(100,255)
		ListAddLast(List,t)
		Return t
	End Function
	
	Method Compare(p:Object)
		pr:mytype= mytype(p)
		
		If(self.value < pr.value) Then
			Return 1
		Else 
			Return 0
		End If
	End Method	
End Type

mytype.Create(12)
mytype.Create(6)
mytype.Create(28)
mytype.Create(128)

List.Sort()

For i:mytype = EachIn list
	Print i.value
	Print i.name
Next


Ahm Filax. Could you add list sorting to your gui?
So that user can sort lists upwards or downwards? :)

Good idea :)

I have try to make a sort method it work ! but the problem is what is
the good method to sort ! example :

I have a listbox with

NAME TITI
NAME TOTO
NAME TUTU
NAME TETE

What can i sort ?? with a listbox containing number's it's really
simple :) example :

10
200
30
400

but with text ? I'm thinking to analyze the first item letter but in the
previous example all item's have the same letter !

An idea ?

Method Compare(p:Object)
pr:mytype= mytype(p)

Return self.text.Compare(pr.text)
End Method

Many thanks fredborg :) alway here to save me :)

It's ok :) now Iglass can sort listbox item's :)

MyList.SortItems()
'MyList.SortItems(False) ' ascending off

I mostly have nice ideas, but cant solve them myself...

I should become creative manager! :D

Lol :)

There's a lot of sortlist threads in the forums but
this one is the one that made me understand it !

Dreamora's explanation and filax simple code-example
is all it took ! Fredborgs string-addon is also good...

Thanks all and *bump* ;o)

Hi guys.

Sorry for mi bad english.
In the thread Little Mod i publish any mod with any replacement for class TMap.
Is posible use it for sort any data. The data entered are automatically sorted. The problem arent the duplicates.

Other class contained are a queue with priority. It have duplicates. Problem are de keys. The keys only accept Integers.

The source is available and accept changes for your adaptation.

Get the mod, docs and source in:

ftp://www.furula.com/rvm.mod.zip

Bye,
Ramon

For this to work for me I had to include the return type of int in the method call like so:

Method Compare:Int(p:Object)