This script will sort the playlist by album then track. Note it will be quite slow for a large number of files as it requires an ML lookup for every item. It would be faster to have it run off a ‘send to’ menu as the ML lookups would already be done. Otherwise, it would be faster to use meta data such as filename or WATitle which don’t require a ML lookup.
Edit CompareMore() and CompareLess() to sort by other criteria.
x = playlist.getselection() if ubound(x,1) > 1 Then Quicksort x, 1, ubound(x,1) End if quit Sub QuickSort(ByRef vec,loBound,hiBound) Dim pivot,loSwap,hiSwap,temp '== Two items to sort if hiBound - loBound = 1 then if CompareMore(vec(loBound), vec(hiBound)) then SwapPls vec(loBound), vec(hiBound) End If End If '== Three or more items to sort set pivot = vec(int((loBound + hiBound) / 2)) SwapPls vec(int((loBound + hiBound) / 2)), vec(loBound) loSwap = loBound + 1 hiSwap = hiBound do '== Find the right loSwap while loSwap < hiSwap and CompareMore(vec(loSwap), pivot) loSwap = loSwap + 1 wend '== Find the right hiSwap while CompareLess(vec(hiSwap), pivot) hiSwap = hiSwap - 1 wend '== Swap values if loSwap is less then hiSwap if loSwap < hiSwap then SwapPls vec(loSwap), vec(hiSwap) End If loop while loSwap < hiSwap SwapPls vec(loBound), vec(hiSwap) SwapPls pivot, vec(hiSwap) '== Recursively call function .. the beauty of Quicksort '== 2 or more items in first section if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1) '== 2 or more items in second section if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound) End Sub 'QuickSort 'A swap that also changes the .position property of a mediaitem 'And positions in the playlist Sub SwapPls (ByRef val1, ByRef val2) tmpos = val1.position tmpos2 = val2.position 'Update new positions val1.position = tmpos2 val2.position = tmpos 'Swap items set temp = val2 set val2 = val1 set val1 = temp 'Swap positions in the playlist playlist.swapindex tmpos2, tmpos End Sub Function CompareLess (ByRef val1, ByRef val2) if (StrComp(val1.album, val2.album, vbTextCompare) > 0) then CompareLess = True Exit Function end if if (StrComp(val1.album, val2.album, vbTextCompare) = 0) then if (val1.track > val2.track) then CompareLess = True Exit Function end if end if CompareLess = False End Function Function CompareMore (ByRef val1, ByRef val2) if (StrComp(val1.album, val2.album, vbTextCompare) < 0) then CompareMore = True Exit Function end if if (StrComp(val1.album, val2.album, vbTextCompare) = 0) then if (val1.track <= val2.track) then CompareMore = True Exit Function end if end if CompareMore = False End Function