1. EuGTK:Sorting Column of Floats

I'm using a gSTR to display a price column, but when I sort, it's sorting alphabetically instead of numerically.

Trying to use gFLT just shows 0.00000000 for every row. getlost

I thought I saw a reference somewhere about sorting columns the proper way depending on the content, but now I can't find it.

I'll keep looking, but how do I sort a column of floats that are actually gSTRs?

See this sample: https://openeuphoria.org/pastey/319.wc

Sort by the Price column.

new topic     » topic index » view message » categorize

2. Re: EuGTK:Sorting Column of Floats

euphoric said...

I'm using a gSTR to display a price column, but when I sort, it's sorting alphabetically instead of numerically.

Trying to use gFLT just shows 0.00000000 for every row. getlost

I thought I saw a reference somewhere about sorting columns the proper way depending on the content, but now I can't find it.

I'll keep looking, but how do I sort a column of floats that are actually gSTRs?

See this sample: https://openeuphoria.org/pastey/319.wc

Sort by the Price column.

The gFLT and gDBL were left out of the change I made when I added the recursive function. Wasn't sure recursion was going to work. Will send an update asap.

That's why I asked earlier whether you needed to use actual atoms/integers, or could deal with string versions of the value.

There are several things at work here: GTK's default display for floats (i.e. dollar amounts) is not good. There are too many decimal places. You need to format the value more neatly, and perhaps add a dollar sign or whatever.

But of course doing that means the sorting is going to be off. Because the sort is based on what you see in the column.

One way is to create 2 columns, one defined as gSTR, the other defined as gFLT. Both get their values from the same column of data.

You hide the gFLT column, but sort on that column when you click on the one with the formatted values.

These hidden columns can be useful in all sorts of ways (control the color or text of the rows, sort on some combination of values, etc. Or finding all categories which have something in common: e.g. you type "shoes" into a search box, and all categories with "shoes" rise to the top: "Mens shoes" "Womens shoes" "Brake shoes" "Shoeshine kits" ...

That's probably the easiest way, but only if the values aren't editable.

Other ways involve writing custom sort routines. Will work on that tonight.

new topic     » goto parent     » topic index » view message » categorize

3. Re: EuGTK:Sorting Column of Floats

irv said...

One way is to create 2 columns, one defined as gSTR, the other defined as gFLT. Both get their values from the same column of data.

You hide the gFLT column, but sort on that column when you click on the one with the formatted values.

That sounds simple and perfect! I'll give that a try when I get back to it.

new topic     » goto parent     » topic index » view message » categorize

4. Re: EuGTK:Sorting Column of Floats

Not as simple as you think. I believe that using a GtkModelFilter and a GtkModelSort to hold the data is a better way.

Probably will need to connect to a custom sort routine for that. Working on that now.

new topic     » goto parent     » topic index » view message » categorize

5. Re: EuGTK:Sorting Column of Floats

OK, I've got the custom sort working. It's not much different from Euphoria's custom sort in operation.

At this point, I need to know more about the actual data you're using to be of much more help. You may also need an update to GtkEngine.e. Briefly, however, you set the sort column id for the Price column to -1. That means "use custom sorting".

col3 = create(GtkColumn, 
   "name=col3,title=Price,type=text,text=3,sort=-1,xalign=1") 
 
constant sorted = create(GtkTreeModelSort,store)   
-- store is where you loaded the raw data; 
-- sorted holds no data, just tracks the sorting... 
 
set(tv,"model",sorted)  
-- use the sorted model to fill your tree model. 
-- the sorted model "ghosts" the real data and keeps it 
-- sorted based on your custom routines. 
 
connect(col3,"clicked","ToggleSortDir")  
-- the above call toggles the sort direction, and calls the custom  
-- sort functions, which are entirely dependent upon the structure 
-- of the data, no point in trying to guess that! 
 

Prices in the input data are atoms. Not quoted.

I specify the Price column as gSTR, so that numbers are automatically formatted with 2 decimal places. The custom routines need to convert the 2 values they compare into atoms for a proper comparison. This is easy and much faster than trying to format all numbers "on the fly".

new topic     » goto parent     » topic index » view message » categorize

6. Re: EuGTK:Sorting Column of Floats

irv said...

OK, I've got the custom sort working. It's not much different from Euphoria's custom sort in operation.

At this point, I need to know more about the actual data you're using to be of much more help.

It's a column of prices.

Do you have the latest GtkEngine.e available for download anywhere? smile

new topic     » goto parent     » topic index » view message » categorize

7. Re: EuGTK:Sorting Column of Floats

Well, here are some of the things to be aware of:

If you use the GtkTreeModel sort, it sorts prices within each category:

For example, if your data looks like that shown below, then when you click sort, low-to-high, it sorts them as:

  • Saws
    • 8 inch blade 59.95
    • Sawzall 139.95
  • Drill Press
    • Bench Mounted
      • 6 speeds 128.50
      • 8 speeds 150.00

IOW, sort is restrained within a category/subcategory. Hopefully, that's what you want.

If instead you wanted something different, ignoring categories, say:

  • 8 inch blade 59.95
  • 6 speeds 128.50
  • Sawzall 139.95
  • 8 speeds 150.00

That would need to be done in a different way. Probably involving either a GtkTreeViewFilter or by letting Euphoria do the dirty work and loading the results.

In addition, once you sort on a column other than the Price column, the data order is lost and the price sort can't be done (on that tree model).

As a suggestion, read the GTK docs about the GtkTreeModel and GtkTreeModelSort. It indicates that you can sort and display the same data in a second TreeView without losing the original sorting ability in the first.

I really can't tell you much more unless I have a better idea of what you're wanting to do and how you're planning to do that.

I've posted a copy of GtkEngine.e and a short test program: [https://sites.google.com/site/euphoriagtk/GtkEngine.e.zip]

{"Saws", 
      {"Reciprocal","Sawzall",139.95}, 
      {"Circular","8 inch blade",59.95}, 
      $}, 
{"Drill Press", 
      {"Bench mounted", 
            {"1/4 hp. 6 speeds",128.50}, 
            {"1/2 hp. 8 speeds",150.00}, 
           $}, 
     $},.. 
new topic     » goto parent     » topic index » view message » categorize

8. Re: EuGTK:Sorting Column of Floats

irv said...

IOW, sort is restrained within a category/subcategory. Hopefully, that's what you want.

I don't know where the lines got crossed (I apologize), but I'm definitely not ever going to consider sorting a GtkTreeView with a GtkTreeStore. I have another GtkTreeView with a GtkListStore, and that's the one I want sorted. It has sortable columns.

Thank you for all the help and for being quick on the updates. Much appreciated!

new topic     » goto parent     » topic index » view message » categorize

9. Re: EuGTK:Sorting Column of Floats

Here is a simple Dollar formatting demo which uses a very simple comparison function (see PriceCompare())

https://openeuphoria.org/pastey/321.wc

new topic     » goto parent     » topic index » view message » categorize

10. Re: EuGTK:Sorting Column of Floats

irv said...

Here is a simple Dollar formatting demo which uses a very simple comparison function (see PriceCompare())

https://openeuphoria.org/pastey/321.wc

Yes! Thank you!

new topic     » goto parent     » topic index » view message » categorize

11. Re: EuGTK:Sorting Column of Floats

This is probably better for most things, since it eliminates the need to create an additional (hidden) column for sorting purposes.

There's some overhead to processing each entry in the price column, however. At some point, the number of rows in your data will become large enough that things will slow down, and you may want to try a different method. What that point is, you'll have to find out by trial and error, with real data.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu