Rearrange tab order according to custom sorting order

Hello,
I could use some inspiration:
I wanna sort the currently open tabs according to specific criteria.
I am aware of the move() command as well as the sort(…) command for arrays.

I currently am thinking about going like this:
first get all tabs via the query({}) command.
then I wanna use the sort function with a custom comparator as such:
first sort by the “root url part”, namely if a tab has the url http://google.de/abcd
I want the ordering by dependent on the “google.de
doesnt matter if http or https and doesnt matter whatever comes after the first singular /.

then if stuff is sorted that way, I then, where possible, want to order it further by the text displayed in the url bar, most likely the “title” attribute I would guess.

so namely, given 2 tabs a and b, I want one to be “higher” than the other if first of their root url part is lexicographically sorted (just alphabet wise sorting).
if they have the same base url., then they are sorted by title.

now after sorting everything (or at least have an array of the sorted tabs), I somehow need to move() all the tabs in a way so they in the end end up in the sorted positions.

I have a vague idea about this whole process but I am deeply struggling with certain parts.

for example how to move things.
cause if we wanna rearrange tabs in the order 4,1,3,2,5, how do you move what to achieve it?
cause if I place the 4th tab in the first position (yeah, index are 0 based in reality I know)
then the whole indizes of the other tabs changes and my “list of positions” has been rendered useless :-/

I jsut dont have the big mental click moment.
Can someone give me some good hints on how to do this plan? :slight_smile:

Hi @bernd7

I’m not an extension developer so I could be completely wrong. :slightly_smiling_face:

If I understand correctly you’ll have a sorted array of Tab objects after your sorting operations and this array has no connection to the actual tabs. (In the meaning of: The sorting didn’t effect the actual tabs directly)

Based on this and the fact that the move() method uses the tab id (not the index): Shouldn’t you now be able to loop over your sorted array, get the unique id of the current tab and move() it to the current index of the loop? Since the move() uses the id and not the index of the actual tab, it shouldn’t matter that the indexes of the actual tabs are changing with every move().

Let me now if I completely missed the point :grin:

Have a nice weekend,
Michael

Yeah, I dont know how to express it properly, so let me give an example:
Say we initially have an array of tab indizes. (wether you have an array of tabs, tab indizes or tab ids doesnt matter sicne you can easily built one from the other).
Like array A=(0,1,2,3,4,5,6)
So an array where each element has a value equal to it’s index.

now, due to some comparison reason, we find that the element at index 6 is “smaller” then the one at index 2.
so we “move” the index=6 element to the (previous) index=2 position and get:
A’=(0,1,6,2,3,4,5)
Now what has changed?
elements at index 0-1 are unchanged.
former 6 element now is at index 2 as intended.
however the elements 2-5, even though they werent invovled in the 2 vs 6 comparison, have changed index.
2 is at 3, 3 at 4, etc.

So 2-5 changed an index position upwards.

so here with a single moving the side effects can still be observed, however for many many movements one after a other it is unpredictable what will end up where.

like for example if we went ahead and from (0,1,2,3,4,5,6) sent 6 to index=2 as above, so we have (0,1,6,2,3,4,5).
and then want to sent 4 to index=1.
so we get
(0,4,1,6,2,3,5)
this shows the problem already:
by now 6 isnt at 2 anymore as we wanted.

so the side effects are pretty devastating and make movements hinder each other.

Given a final permutation list like (4,3,5,1,2,0) (that tells us that the 0th tab should now be at index=5, 1 should now be at index=3, 2 should now be at index=4, 3 should now be at index=2, etc.) I see potentially 2 possible ways to handle it:

1)built the new tab list by moving indexwise from left to right,
so moving 4 to index=0, moving 3 to index=1, moving 5 to index=2, moving 1 to index=3, moving 2 to index=4,moving 0 to index=5.
so doing the needed move operations based on the index of the final lsit.
2) go the not-efficient way and also take stuff from left to right, so former 4,3,5,1,2,0 in that order, simply at the end of the line.
after going through it all, stuff will jsut end up at the right position by itself this way :-/

Sa thing is that both ways dont take into account the former positioning, like if all stuff is in the right positions except for just 1 or 2 things, it still will have to move all items once.

I dont know I jsut have no really good plan to move stuff. :-/

But the changed index doesn’t matter since move() selects the tab by tabId (not index), doesn’t it?

let moving = browser.tabs.move(
  tabIds,              // integer or integer array
  moveProperties       // object
)

When we have an array of sorted tabIds, we can use the tabId to select the tab and the index of the array to determine the move target. When we loop through the array we move the tabs to the target spots from left to right (indexes 0 to array.length -1).

It’s true that we issue a move() command to every tab even if there are no changes at all, but to know how efficient (or inefficient) that is, we need to measure the performance. (How long does it take for 100 tabs?, 1000 tabs?).