RE: [gtk-list] glib: how to sort a doubly linked list?




On 01-Jun-98 Joel Wijngaarde wrote:
> Hi
>  
> i noticed a function in glib called:
>       GList* g_list_insert_sorted (GList *list, gpointer data,
>                                    GCompareFunc func)
> 
> I'm wondering how i can use this function on a GList which contains
> a
> pointer to a structure. The structure looks like this:
> 
<snip>
> 
> Now i want to sort the GList alphabetically, looking at the 'name'
> in
> the data structure... Is there a way i can achieve this with the
> function above?

You can sort the list in anyway you want, as the last argument to
g_list_insert_sorted is a pointer to a function that *you* provide
that does the comparison of two elements.

In your case, this function may look something like:

gint my_struct_compare(MyStruct *a, MyStruct *b)
{
     return(strcasecmp(a->name, b->name));
}

and then the call looks something like:

  mylist = g_list_insert_sorted(mylist, new_element,
                    (GCompareFunc) my_struct_compare);
 
have a look at glib/testglib.c for further help.

Tony


---
E-Mail: trog@gtk.org
Slow day.  Practice crawling.

Go Bezerk! http://www.gtk.org/~trog



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]