Javascript Interactive Merge Sort.
The sorting algorithm where you are the compare function.
This small JS library is useful when trying to sort or prioritize a list of items for which the comparison operation is transitive but cannot be determined algorithmcally. Ie:
a < b and b < c => a < c
but a computer cannot tell that a < b
.
In that case, the two items are displayed to the user which acts as a compare function by selecting which one holds a greater value. Based on these comparisons, the library performs merge sort in the background.
For example, I use it to sort my list of video games played and determine my top 10. You can find the demo here.
The library can be used a such:
var sorter = new Jims(elements, displayLeft, displayRight, showResult, top);
where
elements
is an array of elements to sortdisplayLeft
anddisplayRight
are functions taking as a parameter an item out of the elements array and display that element on the left or right side of the screen respectivelyshowResult
is the callback function that will be called with the sorted list of elements once the process is donetop
is an optional parameter indicating that you only care about the top n elements of the array to sort. Note that a smaller top means less comparison for the user as lower valued elements are discarded. By default,top
is set to the length ofelements
The sorter
object then provides two picker functions which you should hook up to the user's click on the left and right side element respectively:
var picker = sorter.pickerFunctions();
$('#left').click(picker.left);
$('#right').click(picker.right);
Once triggered, these picker functions will step forward with the comparison and use the displayLeft
and displayRight
functions to present a new combination to the user.