Skip to content

Instantly share code, notes, and snippets.

@Yannx79
Created November 25, 2024 02:06
Show Gist options
  • Save Yannx79/fc2c6102480dab48fbcc6a32fe574f60 to your computer and use it in GitHub Desktop.
Save Yannx79/fc2c6102480dab48fbcc6a32fe574f60 to your computer and use it in GitHub Desktop.
// This is Pseudocode
function adjustOrder(list):
// Step 1: Adjust invalid values
for each element in list:
if element.order <= 0:
element.order = null // Mark as "no order"
// Step 2: Sort the list by 'order', moving null to the end
sort list by:
if a.order is null then return 1
if b.order is null then return -1
return a.order - b.order
// Step 3: Assign consecutive values to the 'order' field
currentOrder = 1
for each element in list:
element.order = currentOrder
currentOrder = currentOrder + 1
return list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment