# Example of sorted, filtered list

**URL:** <https://www.ruby-forum.com/t/example-of-sorted-filtered-list/62362>\
**Category:** Ruby-Gnome 2\
**Created:** [June 3, 2006, 8:39am UTC](https://www.ruby-forum.com/t/example-of-sorted-filtered-list/62362 "2006-06-03T08:39:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 3, 2006, 8:39am UTC](https://www.ruby-forum.com/t/example-of-sorted-filtered-list/62362/1 "2006-06-03T08:39:47Z")

</div>

Perhaps someone will find this useful – perhaps it could be put into  
the Gtk examples.

# 

# A brief demonstration of

# - Gtk::Window

# - Gtk::ScrolledWindow

# - Gtk::ListStore

# - Gtk::TreeView

# - Gtk::TreeModelSort

# - Gtk::TreeModelFilter

# - Gtk::CellRenderText

# - Gtk::TreeViewcolumn

# - Gtk::TreeIter

# 

# By Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

# This is released into the Public Domain

# 

require ‘gtk2’

# Create a list store with:

# - an Integer (ID)

# - a String (date)

# - a TrueClass (represents whether or not the row should be shown or

not)  
list\_store = Gtk::ListStore.new Integer, String, TrueClass

# Create a filtered list store that wraps the list store

# and set the filter column criteria

list\_store\_filter = Gtk::TreeModelFilter.new list\_store  
list\_store\_filter.visible\_column = 2

# Create a sorted list store and set the sorting function for the

# first column. Note that we are wrapping the sorted model around

# filtered model – which in turn wraps the original list store model.

list\_store\_sort = Gtk::TreeModelSort.new list\_store\_filter  
list\_store\_sort.set\_sort\_func(0) { |i, j| i[0] \<=\> j[0] }

# Fill the list store with stuff

10.times do |i|  
iter = list\_store.append  
iter[0] = i  
iter[1] = Time.now.to\_s  
iter[2] = true  
end

# Start Gtk

Gtk::init

# Create a window with a size of 300x300 pixels

window = Gtk::Window.new  
window.set\_size\_request 300, 300  
window.signal\_connect(“destroy”) { Gtk::main\_quit }

# Create a scrollable window, with automatic scrollbars, and

# pack it into the window

scrolled\_window = Gtk::ScrolledWindow.new  
scrolled\_window.set\_policy Gtk::POLICY\_AUTOMATIC, Gtk::POLICY\_AUTOMATIC  
window \<\< scrolled\_window

# Create a treeview, giving it the sorted list store (which is wrapping

the  
tree\_view = Gtk::TreeView.new list\_store\_sort  
scrolled\_window.add\_with\_viewport tree\_view

# Create a CellRenderer that’s in charge of rendering the ID and the

renderer = Gtk::CellRendererText.new

# Create the ID column which uses the cell renderer defined above and

# is hooked up to the first column of the model. Sorting is turned on

# on this column (which also uses the first column in the model to

figure

# out what to sort)

id\_column = Gtk::TreeViewColumn.new “ID”, renderer, :text =\> 0  
id\_column.sort\_column\_id = 0  
tree\_view.append\_column id\_column

# Create a column that renders the current time (the second column in

# the model

time\_column = Gtk::TreeViewColumn.new “Current Time”, renderer, :text =\>  
1  
tree\_view.append\_column time\_column

# Shows everything

window.show\_all

# Twice a second, we loop through each row in the list and update the

time

# and set half of the rows’ visibility attribute to false and the other

# half to true

Gtk::timeout\_add(500) do  
list\_store.each do |model, path, iter|  
iter[1] = Time.now.to\_s  
iter[2] = rand(2) == 1  
end  
end

# Starts the Gtk main loop

Gtk::main
