Trier Les Entiers d'une Listview [Résolu]

Messages postés
26
Date d'inscription
mardi 15 juin 2004
Dernière intervention
29 octobre 2008
- 21 juil. 2008 à 14:01 - Dernière réponse :
Messages postés
3288
Date d'inscription
jeudi 3 avril 2008
Dernière intervention
14 septembre 2014
- 21 juil. 2008 à 17:49
Bonjour,



<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>
 




Un petit blèm pour trier les entiers (Integers) avec <?xml:namespace prefix st1 ns "urn:schemas-microsoft-com:office:smarttags" /??><st1:personname w:st="on" productid="la class Compare">la class Compare</st1:personname> dans Listview.



 




1


10


11


13


2


23


24


25



3
30 




Le code que j’ai trouvé sur msdn trie les strings et dates.


Comme faire pour trier les entiers ?



 




Exemple


1


2


310


11


13


23


24


25



30
...
 




Voici le Code



 





Public

Function Compare(ByVal x AsObject, ByVal y AsObject) AsIntegerImplements System.Collections.IComparer.Compare






           
Try







              










               
' Parse the two objects passed as a parameter as a DateTime.







               
Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)






               
Dim secondDate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)






               
' Compare the two dates.







                returnVal = DateTime.Compare(firstDate, secondDate)






               
' If neither compared object has a valid date format,







               
' compare as a string.







           
Catch







               
' Compare the two items as a string.







                returnVal = [String].Compare(CType(x, _






                                  ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)






           
End
Try







 







           
' Determine whether the sort order is descending.







     
      
If order = SortOrder.Descending Then






               
' Invert the value returned by String.Compare.







                returnVal *= -1






           
End
If







           
Return returnVal






 







       
End
Function







   

End

Class









 





 




Merci pour votre aide.



 





 







naim1970
Afficher la suite 

Votre réponse

2 réponses

Meilleure réponse
Messages postés
17308
Date d'inscription
mercredi 2 janvier 2002
Dernière intervention
22 août 2018
- 21 juil. 2008 à 14:21
3
Merci
tu effectue le tri que tu souhaites....
les données du listview sont des String.

pour trier des dates, on tentes de les convertir en date :

Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)

pour un tri numérique, c'est exactement identique...

Dim firstInt As System.Integer = Integer.Parse(CType(x, ListViewItem).SubItems(col).Text)


le tout étant d'effectuer ta comparaison, et de renvoyer -1, 0 ou 1 ...

Merci Renfield 3

Avec quelques mots c'est encore mieux Ajouter un commentaire

Codes Sources a aidé 88 internautes ce mois-ci

Commenter la réponse de Renfield
Messages postés
3288
Date d'inscription
jeudi 3 avril 2008
Dernière intervention
14 septembre 2014
- 21 juil. 2008 à 17:49
0
Merci
Imports




System.Globalization



Public








Class

NaturalComparer



Implements

IComparer(


Of





String

)



Implements

IComparer



Private

mParser1


As

StringParser



Private

mParser2


As

StringParser



Private

mNaturalComparerOptions


As

NaturalComparerOptions



Private





Enum

TokenType[Nothing]

Numerical

[String]



End





Enum








Private





Class

StringParser



Private

mTokenType


As

TokenType



Private

mStringValue


As





String








Private

mNumericalValue


As





Decimal








Private

mIdx


As





Integer








Private

mSource


As





String








Private

mLen


As





Integer








Private

mCurChar


As





Char








Private

mNaturalComparer


As

NaturalComparer



Sub





New

(


ByVal

naturalComparer


As

NaturalComparer)mNaturalComparer = naturalComparer



End





Sub








Public





Sub

Init(


ByVal

source


As





String

)



If

source


Is





Nothing





Then

source =


String

.EmptymSource = source

mLen = source.Length

mIdx = -1

mNumericalValue = 0

NextChar()

NextToken()



End





Sub








Public





ReadOnly





Property

TokenType()


As

TokenType



Get








Return

mTokenType



End





Get








End





Property








Public





ReadOnly





Property

NumericalValue()


As





Decimal








Get








If

mTokenType = NaturalComparer.TokenType.Numerical


Then








Return

mNumericalValue



Else








Throw





New

NaturalComparerException(


"Internal Error: NumericalValue called on a non numerical value."

)



End





If








End





Get








End





Property








Public





ReadOnly





Property

StringValue()


As





String








Get








Return

mStringValue



End





Get








End





Property








Public





Sub

NextToken()



Do








'CharUnicodeInfo.GetUnicodeCategory








If

mCurChar =


Nothing





Then


mTokenType = NaturalComparer.TokenType.Nothing


mStringValue =





Nothing








Exit





Sub








ElseIf





Char

.IsDigit(mCurChar)


Then


ParseNumericalValue()








Exit





Sub








ElseIf





Char

.IsLetter(mCurChar)


Then


ParseString()








Exit





Sub








Else








'ignore this character and loop some more


NextChar()








End





If








Loop








End





Sub








Private





Sub

NextChar()mIdx += 1



If

mIdx >= mLen


Then


mCurChar =





Nothing








Else


mCurChar = mSource(mIdx)








End





If








End





Sub








Private





Sub

ParseNumericalValue()



Dim

start


As





Integer

= mIdx



Dim

NumberDecimalSeparator


As





Char

= NumberFormatInfo.CurrentInfo.NumberDecimalSeparator(0)



Dim

NumberGroupSeparator


As





Char

= NumberFormatInfo.CurrentInfo.NumberGroupSeparator(0)



Do


NextChar()








If

mCurChar = NumberDecimalSeparator


Then








' parse digits after the Decimal Separator








Do


NextChar()








If





Not





Char

.IsDigit(mCurChar)


AndAlso

mCurChar <> NumberGroupSeparator


Then





Exit





Do








Loop








Exit





Do








Else








If





Not





Char

.IsDigit(mCurChar)


AndAlso

mCurChar <> NumberGroupSeparator


Then





Exit





Do








End





If








Loop


mStringValue = mSource.Substring(start, mIdx - start)








If





Decimal

.TryParse(mStringValue, mNumericalValue)


Then


mTokenType = NaturalComparer.TokenType.Numerical








Else








' We probably have a too long value


mTokenType = NaturalComparer.TokenType.String








End





If








End





Sub








Private





Sub

ParseString()



Dim

start


As





Integer

= mIdx



Dim

roman


As





Boolean

= (mNaturalComparer.mNaturalComparerOptions


And

NaturalComparerOptions.RomanNumbers) <> 0



Dim

romanValue


As





Integer








Dim

lastRoman


As





Integer

=


Integer

.MaxValue



Dim

cptLastRoman


As





Integer








Do








If

roman


Then








Dim

thisRomanValue


As





Integer

= RomanLetterValue(mCurChar)



If

thisRomanValue > 0


Then








Dim

handled


As





Boolean

=


False








If

(thisRomanValue = 1


OrElse

thisRomanValue = 10


OrElse

thisRomanValue = 100)


Then


NextChar()








Dim

nextRomanValue


As





Integer

= RomanLetterValue(mCurChar)



If

nextRomanValue = thisRomanValue * 10


Or

nextRomanValue = thisRomanValue * 5


Then


handled =





True








If

nextRomanValue <= lastRoman


Then


romanValue += nextRomanValue - thisRomanValue


NextChar()


lastRoman = thisRomanValue \ 10


cptLastRoman = 0








Else


roman =





False








End





If








End





If








Else


NextChar()








End





If








If





Not

handled


Then








If

thisRomanValue <= lastRoman


Then


romanValue += thisRomanValue








If

lastRoman = thisRomanValue


Then


cptLastRoman += 1








Select





Case

thisRomanValue



Case

1, 10, 100



If

cptLastRoman > 4


Then

roman =


False








Case

5, 50, 500



If

cptLastRoman > 1


Then

roman =


False








End





Select








Else


lastRoman = thisRomanValue


cptLastRoman = 1








End





If








Else


roman =





False








End





If








End





If








Else


roman =





False








End





If








Else


NextChar()








End





If








If





Not





Char

.IsLetter(mCurChar)


Then





Exit





Do








Loop


mStringValue = mSource.Substring(start, mIdx - start)








If

roman


Then


mNumericalValue = romanValue


mTokenType = NaturalComparer.TokenType.Numerical








Else


mTokenType = NaturalComparer.TokenType.String








End





If








End





Sub








End





Class








Sub





New

(


ByVal

NaturalComparerOptions


As

NaturalComparerOptions)mNaturalComparerOptions = NaturalComparerOptions

mParser1 =



New

StringParser(


Me

)mParser2 =



New

StringParser(


Me

)



End





Sub








Sub





New

()



MyClass

.New(NaturalComparerOptions.Default)



End





Sub








Public





Function

Compare(


ByVal

string1


As





String

,


ByVal

string2


As





String

)


As





Integer





Implements

System.Collections.Generic.IComparer(


Of





String

).ComparemParser1.Init(string1)

mParser2.Init(string2)



Dim

result


As





Integer








Do








If

mParser1.TokenType = TokenType.Numerical


And

mParser2.TokenType = TokenType.Numerical


Then








' both string1 and string2 are numerical


result =





Decimal

.Compare(mParser1.NumericalValue, mParser2.NumericalValue)



Else


result =





String

.Compare(mParser1.StringValue, mParser2.StringValue)



End





If








If

result <> 0


Then








Return

result



Else


mParser1.NextToken()


mParser2.NextToken()








End





If








Loop





Until

mParser1.TokenType = TokenType.Nothing


And

mParser2.TokenType = TokenType.Nothing



Return

0


'identical








End





Function








Private





Shared





Function

RomanLetterValue(


ByVal

c


As





Char

)


As





Integer








Select





Case

c



Case

"I"c



Return

1



Case

"V"c



Return

5



Case

"X"c



Return

10



Case

"L"c



Return

50



Case

"C"c



Return

100



Case

"D"c



Return

500



Case

"M"c



Return

1000



Case





Else








Return

0



End





Select








End





Function








Public





Function

RomanValue(


ByVal

string1


As





String

)


As





Integer


mParser1.Init(string1)








If

mParser1.TokenType = TokenType.Numerical


Then








Return





CInt

(mParser1.NumericalValue)



Else








Return

0



End





If








End





Function








Public





Function

IComparer_Compare(


ByVal

x


As





Object

,


ByVal

y


As





Object

)


As





Integer





Implements

System.Collections.IComparer.Compare



Return

Compare(


DirectCast

(x,


String

),


DirectCast

(x,


String

))



End





FunctionEnd







Class


<System.Flags()>





Public





Enum

NaturalComparerOptionsNone

RomanNumbers



'DecimalValues <- we could put this as an option








'IgnoreSpaces <- we could put this as an option








'IgnorePunctuation <- we could put this as an option


[Default] = None






End








EnumPublic







Class

NaturalComparerException



Inherits

Exception



Sub





New

(


ByVal

msg


As





String

)



MyBase

.New(msg)



End





SubEnd







Class









Au contraire des chasseurs qui ne sont pas des lapins, les pollueurs, eux, sont des ordures



/B>
Commenter la réponse de gillardg

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.