Board index » delphi » TStringList and sorting...

TStringList and sorting...

Well, I have a string list which I add numbers to randomly. I wish to
display these numbers in order from smallest to largest. For the most
part, this works fine. But sometimes I run into an obvious unordered
list.

Example:
--------------

After adding the numbers randomly, they come out like this when the list
is sorted:
0 + 1 + 2 + 3 + 4 + 5

On occasion, and I do not know why, they will come out like this:
0 + 1 + 2 + 7 + 4 + 5

I want to stop that if at all possible. I need them in order from the
smallest to the largest, and it must be consistent.

Any ideas on this? I did a search on google, but did not come up with a
solution from there.

Many thanks.

Ryan

 

Re:TStringList and sorting...


Quote
On Fri, 07 Jun 2002 14:09:02 GMT, Ryan Styles <r...@r.ru> wrote:
> Well, I have a string list which I add numbers to randomly. I wish to
> display these numbers in order from smallest to largest. For the most
> part, this works fine. But sometimes I run into an obvious unordered
> list.

It would help if you show some code, i.e. how do you create the list,
where do you get the numbers (and in what format), how do you add them
to the list, how do you sort the list, and how do you read the nubmers
from the list, and display them.

Re:TStringList and sorting...


TStringList already has sorting capabilities (just set Sorted to True). The
only problem is that is sorts texts, not numbers (e.g. '10' is less than
'2'). You can avoid this by prefixing the number with '0's (e.g. '00010' is
larger than '00002').
Another option woult be to use the CustomSort method and provide a provide a
compare function that compares numbers instead of strings.

"Ryan Styles" <r...@r.ru> schreef in bericht
news:dff1gukl9gpadfc9r32g217b2gkl6ssesh@4ax.com...

Quote
> Well, I have a string list which I add numbers to randomly. I wish to
> display these numbers in order from smallest to largest. For the most
> part, this works fine. But sometimes I run into an obvious unordered
> list.

> Example:
> --------------

> After adding the numbers randomly, they come out like this when the list
> is sorted:
> 0 + 1 + 2 + 3 + 4 + 5

> On occasion, and I do not know why, they will come out like this:
> 0 + 1 + 2 + 7 + 4 + 5

> I want to stop that if at all possible. I need them in order from the
> smallest to the largest, and it must be consistent.

> Any ideas on this? I did a search on google, but did not come up with a
> solution from there.

> Many thanks.

> Ryan

Re:TStringList and sorting...


On Sat, 8 Jun 2002 09:22:18 +0200,  Menno Avegaart, apparently lost,
wandered into the group and said:

Quote
>TStringList already has sorting capabilities (just set Sorted to True). The
>only problem is that is sorts texts, not numbers (e.g. '10' is less than
>'2'). You can avoid this by prefixing the number with '0's (e.g. '00010' is
>larger than '00002').
>Another option woult be to use the CustomSort method and provide a provide a
>compare function that compares numbers instead of strings.

Yes, I know it already has sorting capabilities. I have it sorted
because I am using the dupIgnore option which does not work unless the
StringList is sorted.

The problem was that sometimes, 2 would come out AFTER the number 13 or
something larger. But I think you may have hit on the solution I needed
with the leading zero's. I didn't even think to try it.

Thanks!

Ryan

Other Threads