Skip to content

SteveGBanton/lexo-sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

lexo-sort

Lexicographic sorting algorithm for list sorting.

Use this package if you need to be able to keep a sorted list but only want to update one item at a time when an item is moved up or down the list.

Ideal for things like trello boards and very long task lists, especially when synchronized live across many clients.

Usage

Add a 'sortKey' column to your database table or to your document. Index it to leverage your database's ability to sort in alphabetical order.

There are two relevant functions to import and use in your code.

resortEntireList

If migrating data or if the list been resorted too many times (see note below), use the resortEntireList function to generate new sortKeys for your entire list of items.

insertBetween

This function takes two arguments, a beforeItem and afterItem, which are both sort keys.

When moving an item in between two items, provide the sortKey of the item before and the item afterward, and this function will return a computed value for an item in between both.

Update the item being moved with returned sortKey.

If the item is being moved to the top of the list, only provide the afterItem.

If the item is being moved to the bottom of the list, only provide the beforeItem.

Too many resorts

The only downside to this algorithm is that if items are moved up and down around the same area too often, away from the top or bottom of the list, the sortKey may become very long.

This will take a very long time to happen, so it is unlikely to occur in most situations. But if the sortKey becomes longer than say, 150 characters, performance may suffer.

I recommend adding a check for the length of the sortKey on update, and if too long, use resortEntireList to update the entire list with new sortKeys.

About

Lexicographic sorting algorithm for list sorting.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published