+/* Comparison functions */
+
+/**
+ * Return the comparison function appropriate for the SORT_* and ORDER_*
+ * arguments given, or NULL for SORT_RANDOM.
+ * @param i_mode: a SORT_* enum indicating the field to sort on
+ * @param i_type: ORDER_NORMAL or ORDER_REVERSE
+ * @return function pointer, or NULL for SORT_RANDOM or invalid input
+ */
+typedef int (*sortfn_t)(const void *,const void *);
+static const sortfn_t sorting_fns[NUM_SORT_FNS][2];
+static inline sortfn_t find_sorting_fn( unsigned i_mode, unsigned i_type )
+{
+ if( i_mode>=NUM_SORT_FNS || i_type>1 )
+ return 0;
+ return sorting_fns[i_mode][i_type];
+}
+
+/**
+ * Sort an array of items recursively
+ * @param i_items: number of items
+ * @param pp_items: the array of items
+ * @param i_mode: a SORT_* enum indicating the field to sort on
+ * @param i_type: ORDER_NORMAL or ORDER_REVERSE
+ * @return nothing
+ */
+static inline
+void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items,
+ unsigned i_mode, unsigned i_type )
+{
+ sortfn_t sortfn = find_sorting_fn(i_mode, i_type);
+
+ if( sortfn )
+ {
+ qsort( pp_items, i_items, sizeof( pp_items[0] ), sortfn );
+ }
+ else /* Randomise */
+ {
+ int i_position;
+ playlist_item_t *p_temp;
+
+ for( i_position = 0; i_position < i_items ; i_position++ )
+ {
+ int i_new;
+
+ if( i_items > 1 )
+ i_new = rand() % (i_items - 1);
+ else
+ i_new = 0;
+ p_temp = pp_items[i_position];
+ pp_items[i_position] = pp_items[i_new];
+ pp_items[i_new] = p_temp;
+ }
+ }
+}
+