HydrogenAudio

Hosted Forums => foobar2000 => Development - (fb2k) => Topic started by: hyperblast on 2018-03-15 16:15:08

Title: Sorting playlist items in descending order
Post by: hyperblast on 2018-03-15 16:15:08
Hi all,

I want to sort playlist items using API, playlist_manager::playlist_sort_by_format() works well, but I can't find proper way of doing this in descending order.

First I tried to do ascending sort and then reverse order of items, but this has unintentional effect of reordering items that are equal according to sorting title format expression. It other words it makes sorting unstable.

Now I'm doing the following trick: reverse order of items, apply sorting, reverse it again. This makes sorting stable, but looks kinda ugly.

Is there any better way?

Title: Re: Sorting playlist items in descending order
Post by: Peter on 2018-03-15 16:39:15
Apparently there's no direction argument there.
However, the sorting can be accomplished by producing the reorder permutation on your end and applying it to the playlist, which - as far as the rest of the app is concerned - is the same as sorting the playlist using playlist_sort_by_format(). Also, lower level metadb_handle_list sorting methods do have the "direction" parameter so you can perform a reverse sort cleanly.

Code: [Select]
		// Direction, -1 to reverse
int direction = 1;

// Prepare sorter object
titleformat_object::ptr script;
if (!titleformat_compiler::get()->compile( script, "%title%" )) uBugCheck();

metadb_handle_list items; pfc::array_t<size_t> order;
auto api = playlist_manager::get();
// Get whole playlist
api->activeplaylist_get_all_items( items );
// Prepare memory for reorder permutation content
order.set_size_discard( items.get_count() );
// Make our permutation
// Low-level metadb_handle_list_helper method has the direction parameters
metadb_handle_list_helper::sort_by_format_get_order( items, order.get_ptr(), script, nullptr, direction );
// Create an undo snapshot
api->activeplaylist_undo_backup();
// Apply our permutation
api->activeplaylist_reorder_items( order.get_ptr(), order.get_size() );
Title: Re: Sorting playlist items in descending order
Post by: hyperblast on 2018-03-15 16:44:47
...
That is exactly what I wanted, thank you, Peter!