public SequenceIter<G> insert_sorted (owned G data, CompareDataFunc<G> cmp_func)

Inserts data into this using cmp_func to determine the new position.

The sequence must already be sorted according to cmp_func; otherwise the new position of data is undefined.

cmp_func is called with two items of the this, and cmp_data. It should return 0 if the items are equal, a negative value if the first item comes before the second, and a positive value if the second item comes before the first.

Note that when adding a large amount of data to a Sequence, it is more efficient to do unsorted insertions and then call sort or sort_iter.

Example: Insert sorted:

public static int main (string[] args) {
Sequence<string> seq = new Sequence<string> ();
seq.append ("1. Lorem");
seq.append ("2. ipsum");
seq.append ("4. sit");
seq.append ("5. amet");

seq.insert_sorted ("3. dolor", (a, b) => {
return strcmp (a, b);

// Output:
// ``1. Lorem``
// ``2. ipsum``
// ``3. dolor``
// ``4. sit``
// ``5. amet``
seq.foreach ((item) => {
print ("%s\n", item);

return 0;

valac --pkg glib-2.0 GLib.Sequence.insert_sorted.vala



a Sequence


the data to insert


the function used to compare items in the sequence


user data passed to cmp_func.


a SequenceIter pointing to the new item.