I want sort by status [‘ready’,’pending’,’in_progress’,’done’]
The list should display
- data 1 -ready
- data 2 -ready
- data 10 -pending
- data 5 -in_progress
- data 3 -done
I want sort by status [‘ready’,’pending’,’in_progress’,’done’]
The list should display
2
Answers
assuming that you using json as data structure for your data:
this will result:
You should create a JS objects using
reduce()
which allows for quick lookups in constant timeO(1)
which holds the priority or sort position assigned to a value.Then using that object you can easily lookup the sort position and sort accordingly. The overall runtime of the algorithm is
O(n)
. If you use something likeindexOf()
the runtime will beO(n²)
sinceindexOf()
does a linear search.I am using the relatively new
toSorted()
instead of the oldersort()
method here sincetoSorted()
returns a new array and is therefore the immutable option, which should generally be preferred. Browser support is not especially good as of now, so by reason of that you might need to fallback tosort()
.