Copare function
BlitzMax Forums/BlitzMax Programming/Copare function
| ||
Hi there I'm trying to compare string elements ( to sort lists ) and now I come up to this code : Type item Field _st:String End Type Local o1:Item=New Item; o1._st="machin" Local o2:Item=New Item; o2._st="truc" Local o3:Item=New Item; o3._st="bidule" Local o4:Item=New Item; o4._st="turc" Local o5:Item=New Item; o5._st="2cac" Local o6:Item=New Item; o6._st="2six" Local o7:Item=New Item; o7._st="3bla" Local o8:Item=New Item; o8._st="mach1" Local o9:Item=New Item; o9._st="m" Local oA:Item=New Item; oA._st="mb" Print " Compare "+o1._st+" => "+o2._st+" === "+Item(STR_Compare(o1,o2,o1._st,o2._st) )._st Print " Compare "+o1._st+" => "+o3._st+" === "+Item(STR_Compare(o1,o3,o1._st,o3._st) )._st Print " Compare "+o1._st+" => "+o4._st+" === "+Item(STR_Compare(o1,o4,o1._st,o4._st) )._st Print " Compare "+o1._st+" => "+o5._st+" === "+Item(STR_Compare(o1,o5,o1._st,o5._st) )._st Print " Compare "+o5._st+" => "+o6._st+" === "+Item(STR_Compare(o5,o6,o5._st,o6._st) )._st Print " Compare "+o6._st+" => "+o5._st+" === "+Item(STR_Compare(o6,o5,o6._st,o5._st) )._st Print " Compare "+o1._st+" => "+o8._st+" === "+Item(STR_Compare(o1,o8,o1._st,o8._st) )._st Print " Compare "+o1._st+" => "+o9._st+" === "+Item(STR_Compare(o1,o9,o1._st,o9._st) )._st Print " Compare "+o1._st+" => "+oA._st+" === "+Item(STR_Compare(o1,oA,o1._st,oA._st) )._st Const C_STR_ASC_A:Int=Asc("A") Const C_STR_ASC_Z:Int=Asc("z") Function STR_Compare:Object(Obj1:Object,Obj2:Object,name1:String,name2:String) Local Let1:Int=0 ' caractere item 1 Local Let2:Int=0 ' caractere item 2 If Len(name1)=0 Return Obj1 If Len(name2)=0 Return Obj2 Let1=Asc(Left(Upper(name1),1)) Let2=Asc(Left(Upper(name2),1)) ' premier = nombre ? If Let1<C_STR_ASC_A' Or Let1>C_STR_ASC_Z ' si Let2 = nombre => compare les nombre... If Let2<C_STR_ASC_A' Or Let2>C_STR_ASC_Z ' sinon => le chiffre gagne ! If Int(Let1)=Int(Let2) ' egaux, => recursif sur les string sans le premier caractere Return STR_Compare(Obj1,Obj2,Right(name1,Len(name1)-1),Right(name2,Len(name2)-1)) ElseIf Int(Let1)<Int(Let2) ' premier inferieur ! Return Obj1 Else ' seconde inferieur Return Obj2 EndIf Else Return Obj1 EndIf Else ' lettre = caractere ... mais lettre2 = chiffre If Let2<C_STR_ASC_A' Or Let2>C_STR_ASC_Z Return Obj2 Else If Let1=Let2 Return STR_Compare(Obj1,Obj2,Right(name1,Len(name1)-1),Right(name2,Len(name2)-1)) If Let1<Let2 Return Obj1 Else Return Obj2 EndIf EndIf EndIf End Function I coded at 5 am ... so I'm not really sleepy, maybe i made big mistakes I was wondering : Is there a better way to compare strings ? ( don't halt on the object return, I need it ) Notice => I commented "z" test and i can be a mistake... but for some reason, we don't expect each time to compare the whole weyboard chars ... if we only use digit and alpha, it works fine |
| ||
You could use a buildin list: You even can create your own compare function to sort the list, but strings can be sorted out of the box. If I remember right there's an example in the official docs. Can't look right now as my docs are scrambled :( |