7

I'm trying to implement a function in typescript where the user can pass an array of objects and a property of one of the objects and return the list order by the property the user have chosen.

function OrderByArray(values: any[], orderType: any) { 
    //TODO: implement code
    return values;
} 

Is any way to do it? I know how to sort an array but I don't know how to allow to send the typeof object and order by it.

Thanks!

5
  • Why you need to allow types as arguments? It's supposed to represent the sorted property? Commented Nov 6, 2016 at 15:54
  • The reason is because I'm working on a function where I have to receive an array and I don't know the object type and I have to return the values sorted by the object.property sent by the user. @Martin Commented Nov 6, 2016 at 15:57
  • Why you don't use just property: string? Commented Nov 6, 2016 at 15:59
  • So, how could I sort the user array[] by a string? I mean think about the user can send a person[], car[]... etc and ask to order by year, name, carbrand.. I don't know if you can get me. @Martin Commented Nov 6, 2016 at 16:07
  • You want to sort the array by multiple properties? Commented Nov 6, 2016 at 16:10

2 Answers 2

14

You can use the Array.sort method:

function OrderByArray(values: any[], orderType: any) { 
    return values.sort((a, b) => {
        if (a[orderType] < b[orderType]) {
            return -1;
        }

        if (a[orderType] > b[orderType]) {
            return 1;
        }

        return 0
    });
}

I'm not sure what's the value of item[orderType], so you might need to change the ifs.


Edit

Using it:

let animals = [{ name: "cat" }, { name: "dog" }, { name: "lion" }];
let cars = [{ manufacturor: "ford" }, { manufacturor: "mazda" }, { manufacturor: "fiat" }];

console.log(OrderByArray(animals, "name").map(item => item.name)); // ["cat", "dog", "lion"]
console.log(OrderByArray(cars, "manufacturor").map(item => item.manufacturor)); // ["fiat", "ford", "mazda"]

(code in playground)


2nd Edit

In typescript 2.1 you'll be able to do:

function OrderByArray<T, K extends keyof T>(values: T[], orderType: K) {
    ...
}

More about keyof.

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

6 Comments

it could work, let me integrate it in my code, but not sure yet. thanks @nitzan-tomer
in the first edit you did could be possible something like -> OrderByArray(values, orderType).map(item => item.orderType) ? @nitzan-tomer
If orderType is a variable then you need to do: OrderByArray(values, orderType).map(item => item[orderType])
great! thanks! I'm pretty new using typescript. @nitzan-tomer
@chris.currin Fixed. It was a while ago, before the feature was actually out, maybe back then it was just keyof T, not sure or remember.
|
0

You probably do not need to return the results of values.sort(...) as mentioned in MDN

Array.sort actually changes, or mutates the array it sorts

Comments

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.