Pastey Simple Dollar column sort

--# Simple ListView - formatting values, sorting
  
include GtkEngine.e  
  
constant NAME = 1, PRICE = 3 
integer name_sort_dir = 0, price_sort_dir = 0 
 
constant fruits = { -- name, qty, price, tooltip;  
	{"Apples",   "doz",	3.50,	"Mackintosh"},  
	{"Cherries", "lb",	2.69,	"Bing"},  
	{"Dates",    "lb",	6.99,	"Medjool"},  
	{"Limes",    "ea",	1.59,	"Key"},  
	{"Oranges",  "ea",     11.79,	"Valencia"},  
	{"Bananas",  "lb",	 .89,	"Dole"},  
	$}  
	  
constant   
	win = create(GtkWindow,"border=10,size=350x350,$destroy=Quit"),  
	pan = create(GtkBox,"orientation=vertical,spacing=10"),  
	lbl = create(GtkLabel,"text=Double-click an item\nClick header to sort"),  
	scrol = create(GtkScrolledWindow),  
	box = create(GtkButtonBox),  
	btn1 = create(GtkButton,"gtk-quit","Quit"),  
	btn2 = create(GtkButton,"gtk-ok",_("ShowChoice")),  
	  
	store = create(GtkListStore,{gSTR,gSTR,gSTR,gSTR}), -- match with data;  
	sorted = create(GtkTreeModelSort,store) 
	 
	set(sorted,"sort func",2,_("PriceCompare")) -- note: 2 is zero based 
     
constant tv = create(GtkTreeView,{-- this is the container for the list  
	{"reorderable",TRUE}, 	    -- can rearrange columns;  
	{"headers clickable",TRUE},  -- can sort by clicking on header button;  
	{"set grid lines",GTK_TREE_VIEW_GRID_LINES_BOTH}, -- appearance;  
	{"rules hint",TRUE},  -- 'shaded' alternate rows (if window theme allows)  
	{"tooltip column",4}, -- column 4 contains the tips;  
	{"connect","row-activated",_("ShowChoice")}}), -- when double-click on row;  
  
	selection = get(tv,"selection"), -- this 'tracks' the current selection;  
  
	col1 = create(GtkColumn,"title=Name,type=text,text=1,sort=1"),  
	col2 = create(GtkColumn,"title=Quantity,type=text,text=2"), --no sort  
	col3 = create(GtkColumn,"title=Price,type=text,text=3,sort=3")  
  
	set(store,"data",fruits)  
	set(tv,"model",sorted)  
  
	add(tv,{col1,col2,col3})  
	add(box,{btn1,btn2})  
	add(win,pan)  
	pack(pan,lbl)  
	add(scrol,tv)  
	pack(pan,scrol,1,1)  
	pack(pan,-box)  
	  
show_all(win)  
main()  
 
------------------------------------------------------------ 
global function PriceCompare(atom model, object a, object b) 
------------------------------------------------------------ 
a = to_number(get(model,"value",a,3)) -- note: 3 is one based 
b = to_number(get(model,"value",b,3)) -- this will be fixed in  
display("[] []",{a,b})  -- the next EuGTK release! 
return compare(a,b) 
return 1 
end function 
 
 ---------------------------------------------------  
global function SetSortCol(atom btn, integer col)  
--------------------------------------------------- 
switch col do  
  
    case NAME then  
	 name_sort_dir = not(name_sort_dir) -- toggle  
	 set(sorted,"sort column id",col,name_sort_dir+1)  
 
    case PRICE then	  
	 price_sort_dir = not(price_sort_dir) 
	 set(sorted,"sort column id",col,price_sort_dir+1)  
		   
 end switch  
  
return 1  
end function  
  
---------------------  
function ShowChoice()  
---------------------  
object choice = get(selection,"selected row data")  
 return Info(win,,text:format("[4] [1]",choice),  
	format("Price: $[3:,,.2] per [2]",choice))  
end function  

1. Comment by irv Aug 08, 2020

When you specify a column to hold floating-point numbers (gFLT), GTK shows the value with 6 digits following the decimal point, e.g. Apples from the above list would be 3.500000 per doz. This looks bad, but will sort correctly using the built-in GTK sort functions.

There are two ways to avoid this problem: create a custom sort, as shown above and declare the column as gSTR, or declare it as gFLT, and use custom formatting. The custom formatting is problematical on Windows, and more difficult to write.