Guidance
指路人
g.yi.org
Guidance Forums / Rapid-Q Basic / How to sort a QStringGrid?

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. How to sort a QStringGrid?
#565
Posted by: OvalX 2002-10-17 17:54:43
I have a little CD-database in QStringGrid (separator ";"). How can I sort rows in QStringGrid (ascend-descend)? There are many columns in QStringGrid (e.g. Album, Interpret, Time, Year,...). Thanx
Message2. QStringList
#567
Posted by: guidance 2002-10-17 18:52:00
Just an idea (utilize Sort method of QStringList):
Steps:
1) swap the key column to the most left
2) grid.SaveToFile (or stream)
3) stringlist.LoadFromFile
4) stringlist.Sort
5) stringlist.SaveToFile
6) grid.LoadFromFile
7) swap the key column back
Message3. Re: QStringList
#572
Posted by: OvalX 2002-10-18 13:43:55
Good Idea! I will try it and write to you about (un)success! Thank you!
Message4. Re: QStringList
#580
Posted by: OvalX 2002-10-21 18:45:48
You are right - the solution is:
------------------------------
'I = number of column
grid.SwapCols (1, I)
FileStream.Open ("sort.txt", fmCreate)
grid.SaveToStream (FileStream, 1, 1, grid.RowCount)
FileStream.Close
Stringlist.LoadFromFile("sort.txt")
StringList.Sort
StringList.SaveToFile("sort.txt")
FileStream.Open ("sort.txt", fmOpenReadWrite)
grid.LoadFromStream(FileStream, 1, 1, grid.RowCount)
FileStream.Close
grid.SwapCols (1, I)
grid.TopRow = 1
-------------------------------
Thank you once more. OvalX
Message5. Re: QStringList
#583
Posted by: guidance 2002-10-21 20:03:29
Thinking about:
1) If the content in key column is not in same length, very common in fact, then the shorter ones will be tailed with the next column, and lead wrong order.
2) If the cell contains the delimiter character, for example ",", then need to escape it first.
3) Use QMemoryStream instead of QFileStream would be better.
Message6. Re: QStringList
#590
Posted by: OvalX 2002-10-22 20:49:50
a) the change between 2 columns was allways correct (uh!)
b) I used ";" separator rather then "," (is not so used)
c) I was trying to use QMemoryStream, but without effect! Mayby I used wrong code, but compilation was never correct... :-( I am glad, the columns change works good with QFileStream, but I will be happier with QMemoryStream (there are no "signs" on disk :-)!
Message7. Re: How to sort a QStringGrid?
#600
Posted by: 2002-10-24 15:02:49
someone gave me this code:

for y1=2 to grid.rowcount-1
    for y2=1 to grid.rowcount-2
     IF grid.cell(col,y2)>grid.cell(col,y1) then
     grid.swaprows(y1, y2)
     END IF
    next y2
    next y1

works well
Message8. Re: How to sort a QStringGrid?
#601
Posted by: OvalX 2002-10-24 16:04:20
Hi, I was trying this code, yes, works well. A little problem: 500 rows was sorted not immediately, but a long time - cca 50 sec (I have Pentium II, 266 MHz, 64MB RAM). Code with using FileStream works 0,5 sec and is finished. Help anybody with MemoryStream?

Dim MemoryStream as QMemoryStream
Dim StringList as QStringList
--------------
blablabla
-------------- SUB SORT --------------------------
grid.SaveToStream (MemoryStream, 1,1,grid.RowCount)   
ShowMessage Str$(MemoryStream.LineCount)
' returns 500 rows - it is OK

' but following
StringList.LoadFromStream(MemoryStream)
ShowMessage Str$(StringList.ItemCount)
' returns 0 - it is WRONG (must be 500!!!) and sorting do not works :-(
---------------------------------------------------
Where is mistake? Help anybody with MemoryStream?
Message9. Re: How to sort a QStringGrid?
#602
Posted by: guidance 2002-10-24 17:41:43
Maybe you need reset the memorystream.Position.
Message10. Re: How to sort a QStringGrid?
#624
Posted by: 2002-10-31 05:07:20
Most sorts work basically the same way:

1) decide which elements to compare
2) compare the elements
3) if out of order, swap them
4) decide if sort finished, if not, start at 1 again.

The things differentiates one type of sort from another is how it decides which elements to compare, the less compares that have to be made, the faster the sort. The earlier example was pretty much worst case.. it compared the first element to all the others, then the second to all the others, and so on.. there are much more efficient sort methods we could use.

The next thing, is comparing the elements, which just decides if they are out of order or not. The method used for this depends on what type of data we are sorting. In the case of a QStringGrid we would need a routine to compare the strings in two cells in a particular column(the sort key column).
One thing you have to make note of here.. when comparing strings "thisisastring" will not sort the same as "this is a string". If you want to sort strings with multiple words you will need to compare them with spaces removed.

Once we've compared the cells, we need to swap them if nescessary. The thing here is that we want to swap not just the cells being used for the sort key, but the entire rows. Luckily QStringGrid has a SwapRows method to do this for us.

The last thing will be part of the sort algorithm we use so I won't go into it.

Now what to do with this? Look up information on some sort algorithms, and write one to work with QStringGrid. I would try CombSort as it is fast and relatively easy to code, we could also try QuickSort, ShellSort, BubbleSort, etc.

I don't know how fast we'd be able to make it. but it's something to try.

Also, I've been working on a CD database myself, so I'd like to see how this turns out.
Message11. Sort function - solution !
#635
Posted by: OvalX 2002-11-04 18:38:23
Hi all, last week I wrote a working function (thanks Guidance for reseting MemoryStream.Position) to sort a column in the QStringGrid. It is very simply, not super, without any messages or options, but works!:
' Example: SortColGrid (Grid1, 5, 1)

http://g.yi.org/f.php?f=4704
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2024-3-29  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0