How to create custom ordering?

Asked

Viewed 86 times

2

I’m looking for a way to create a custom computer.

Let’s say I have the following:

var array = [
    1=>'A',
    2=>'B',
    3=>'C',
    4=>'D',
    5=>'E',
    6=>'F',
]

Now instead of ordering by the Dice or by the value in alphabetical order or the reverse, Gothic to pass my own order. It could be anything the user imagines ex:

ordem = acebdukl
ordem = brvdencst

If the array item does not exist in the order, it must go to the end. If the array item is repeated all repeats must be in sequence.

The language of example does not matter, I only care about the development of the idea.

  • And you have an object or an array as input?

  • @Sergio could be the 2 cases and even a String in the future, but at the moment it would only be for object and array

  • And what do you do to cases where it doesn’t exist? For example kl in the order that does not exist in this array example

  • @Sergio must follow the next, in the same way as an alphabetical ordering

  • Okay, I put an answer. I have to leave, if that’s not what you’re looking for I can adjust it later. There are probably more answers...

2 answers

2


I think this is what you’re looking for: (example with array)

var array = [
    [1, 'A'],
    [2, 'B'],
    [3, 'C'],
    [4, 'D'], [4, 'D'],
    [5, 'E'],
    [6, 'F']
];

function ordenarPor(arr, ordem) {
    return arr.sort(function (a, b) {
        var indexA = ordem.indexOf(a[1].toLowerCase());
        var indexB = ordem.indexOf(b[1].toLowerCase());
        if (indexA < 0) indexA = arr.length + 1;
        if (indexB < 0) indexB = arr.length + 1;
        return indexA > indexB;
    });
}

var nova = ordenarPor(array, 'acebdukl');
console.log(JSON.stringify(nova));

// dá: [[1,"A"],[3,"C"],[5,"E"],[2,"B"],[4,"D"],[4,"D"],[6,"F"]]

jsFiddle: http://jsfiddle.net/7a9waxkx/2/

  • That’s about it, Sergio, only two factors to consider. If the array item does not exist in the order it must go to the end, and if the item is repeated in the array, all repetitions must appear in the final array. Otherwise it works perfectly

  • @Rodrigoborth ok, would that be so? -> http://jsfiddle.net/7a9waxkx/1/

  • Almost now, I only saw that when I have a repeated value and it is to be ordered the same only appears 1 time and should appear all repetitions... the rest is ok

  • @Rodrigoborth Will these array have what types of variables? Elements of DOM? strings? simple objects/arrays? or a bit of everything?

  • They will be elements that will go to DOM later with the criteria that the user wants

  • @Rodrigoborth -> http://jsfiddle.net/7a9waxkx/2/

  • 1

    Perfect, now just update the answer.

  • @Great Rodrigoborth. I added this last code to the answer.

Show 3 more comments

1

In python, these lines solve your problem:

string = 'acebdukl'

organizada = sum([list(filter(lambda key: key if key[1].upper() == letra else None, lista)) for letra in string.upper()], [])
organizada += [item for item in lista if item not in organizada]

print(organizada) 
>>>
[[1, 'A'], [3, 'C'], [5, 'E'], [2, 'B'], [4, 'D'], [4, 'D'], [6, 'F']]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.