Board index » cppbuilder » Method to sort numbers Stored in TStringList?

Method to sort numbers Stored in TStringList?

One column in a TStringGrid contains numerical data in the format
1234|456. Two numerical values separated by a pipe. Theese values are
the Y- and X-values of a barchart. Before I use them in the chart I need
to sort theese 'strings' in numerical order based on the first of theese
two numerical values and then break them apart. However the first value
could be everything from one digit long to six digits. If I load them
into a TStringList I could use it's sort() method but then 10000 will
end up before 150 and so on. What other sorting methods are there that I
could use in this case?

Thanks
Lars

 

Re:Method to sort numbers Stored in TStringList?


I recommend that you ditch TStringList if you can. Use a vector or a
list instead. That way, you can use the sorting routines from the STL.

Harold Howe [TeamB]
http://www.bcbdev.com

Re:Method to sort numbers Stored in TStringList?


Hi, Lars!

Right justify the numeric values and the sort should work fine. This
will require you to pad them with spaces on the left.

------
Mark Cashman (TeamB - C++ Builder), creator of The Temp{*word*203}Doorway at
http://www.temporaldoorway.com
- Original digital art, writing, music and more -
C++ Builder / JBuilder Tips and The C++ Builder Programmer's Webring
(Join us!)
http://www.temporaldoorway.com/programming/index.htm
------

Re:Method to sort numbers Stored in TStringList?


Hi Lars,

Quote
> One column in a TStringGrid contains numerical data in the format
> 1234|456. Two numerical values separated by a pipe. Theese values are
> the Y- and X-values of a barchart. Before I use them in the chart I need
> to sort theese 'strings' in numerical order based on the first of theese
> two numerical values and then break them apart.  What other sorting
> methods are there that I could use in this case?

In addition to the good suggestions by Harold and Mark, you could use a TList
object and a custom sort procedure to help you out here.  The advantage is that
you can sort virtually any type of item since you implement the custom sorting.
Here's an example...

int __fastcall CustomListSortProc(void *lParam1, void *lParam2)
{
    int item1 = (int)lParam1;
    int item2 = (int)lParam2;

    if (item1 == item2) return 0;
    if (item1 < item2) return -1;
    return 1;

Quote
}

void __fastcall TForm1::SortButtonClick(TObject *Sender)
{
    const int col = 1; // for example
    int start = StringGrid1->FixedRows;
    TList *List = new TList();

    // Fill the list
    for (int index = start; index < StringGrid1->RowCount; ++index)
    {
        AnsiString text = StringGrid1->Cells[col][index];
        int sort_int =
            text.SubString(1, text.Pos("|") - 1).ToIntDef(-1);
        List->Add((void *)sort_int);
    }

    // Sort the list
    List->Sort(CustomListSortProc);

    // Extract from the sorted list and place back in the StringGrid
    // or just use the numbers from the List for plotting, etc.
    StringGrid1->Cols[col]->BeginUpdate();
    for (int index = 0; index < List->Count; ++index)
    {
        AnsiString text = StringGrid1->Cells[col][start + index];
        int pos = text.Pos("|");
        StringGrid1->Cells[col][start + index] =
            IntToStr((int)List->Items[index]) +
            text.SubString(pos, text.Length() - pos + 1);
    }
    StringGrid1->Cols[col]->EndUpdate();

    delete List;

Quote
}

Good luck.

--------------------------------------
Damon Chandler

http://bcbcaq.freeservers.com
Answers to <Commonly Asked Questions>
52064929

Other Threads