1

I have an Array of string containing the data retrieved by the Server. I want to sort that Array based on the importance of its elements.

Here is my code:

 // once the server is called, save the information by changing the order
let formattedData: Array<string> = this.$scope.sortResult(data_response, true);

// This is the function that should sort the data
public sortActions = (arrayToSort: Array<string>, firstList: boolean) => {
    if (!arrayToSort || !arrayToSort.length) {
        return;
    }

    let result: Array<string> = [];
    let j: any = null;

    let listOfImportance: any = null;
    if(firstList){
        listOfImportance = ["Smith", "El", "John", "Karl", "Peter"];
    } else {
        listOfImportance = ["John", "Peter", "Karl", "El", "Smith"];
    }

     for (let i = 0, orderLength = listOfImportance.length; i < orderLength; i++) {
         let currentOrder = listOfImportance[i];
         while (-1 != (j = $.inArray(currentOrder, arrayToSort))) {
             result.push(arrayToSort.splice(j, 1)[0]);
         }
         return result.concat(arrayToSort);
     }
}

The problem is that if data_response (so the server's result) is, for example, ["Peter", "John"] the result of sortActions(data_response, true) is ["Peter", "John"], then it didn't sort correctly. In fact, the expected result would be: ["John", "Peter"]

Perhaps the problem is that the server response doesn't contain all the items in the list of importance?

2
  • one possible solution could be: instead of storing only the names, store and object with name and priority , then, for all the data u get, order them based on the priority (which will be just an integer). Commented Nov 2, 2017 at 11:31
  • sortActions(["Peter", "John"], true) returns the expected result ["John", "Peter"] -> jsfiddle.net/pyx4uyv3 Commented Nov 2, 2017 at 11:38

1 Answer 1

3

I think your problem is the line

     return result.concat(arrayToSort);

This should outside the for, last line of the function, in order to add the remaining items only after everything that could be sorted was sorted.

However, I'd suggest you don't reinvent the wheel, and use a default sorting function from the language. First, map the elements using a priority function, like so:

return array.sort((a, b) => priority(a) - priority(b));

The priority function is a function that maps an element to it's priority (an integer), for instance,

const priority = el => listOfImportance.indexOf(el);

Will sort by the order specified in the array; the first element will be priority 0, and the first in the result, the second will be priority 1, and so on.

Sign up to request clarification or add additional context in comments.

6 Comments

What is priority() function? shoud I use that code instead of "return result.concat(arrayToSort)" and outside the for loop?
Putting the return outside of the loop will alone fix your problem, I believe, but I's suggest a better approach; to use the sort function, that knows how to sort integers, and implement simply a priority function, that given an element assigns it a priority; you can use the position on your priority list, for example.
Where does priority() function come from?
@charlietfl He defined it in the third code block using the arrow function syntax.
@NiklasHigi that was added after I asked. It was previously undefined
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.