]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Merge branch 'master' into lpcm_encoder
[vlc] / src / playlist / sort.c
index 67366d6711d829df7c67940e6bdc2febbafc6255..e125b0d112cc639e5b3af8ebdf644526092cf565 100644 (file)
@@ -27,6 +27,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_rand.h>
 #define  VLC_INTERNAL_PLAYLIST_SORT_FUNCTIONS
 #include "vlc_playlist.h"
 #include "playlist_internal.h"
@@ -140,17 +141,13 @@ void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items,
     }
     else /* Randomise */
     {
-        int i_position;
+        unsigned i_position;
+        unsigned i_new;
         playlist_item_t *p_temp;
 
-        for( i_position = 0; i_position < i_items ; i_position++ )
+        for( i_position = i_items - 1; i_position > 0; i_position-- )
         {
-            int i_new;
-
-            if( i_items > 1 )
-                i_new = rand() % (i_items - 1);
-            else
-                i_new = 0;
+            i_new = ((unsigned)vlc_mrand48()) % (i_position+1);
             p_temp = pp_items[i_position];
             pp_items[i_position] = pp_items[i_new];
             pp_items[i_new] = p_temp;
@@ -244,8 +241,11 @@ SORTFN( SORT_DESCRIPTION, first, second )
 
 SORTFN( SORT_DURATION, first, second )
 {
-    return input_item_GetDuration( first->p_input ) -
-           input_item_GetDuration( second->p_input );
+    mtime_t time1 = input_item_GetDuration( first->p_input );
+    mtime_t time2 = input_item_GetDuration( second->p_input );
+    int i_ret = time1 > time2 ? 1 :
+                    ( time1 == time2 ? 0 : -1 );
+    return i_ret;
 }
 
 SORTFN( SORT_GENRE, first, second )