How to reorder objects in an array in typescript?

I am having an array of objects. The objects within the array represent car model and the price. I would like to reorder the objects within the array , so that the list should follow models SEDAN, followed by CITROEN, then SUZUKI , followed by MERCEDES.

The following is my code.

     enum Model {
      SEDAN = 'SEDAN',
      SUZUKI = 'SUZUKI',
    interface CarModelPrice {
      model :  Model;
      price : number;
    const sedan: CarModelPrice = {
      model: Model.SEDAN,
      price: 25
    const mercedes: CarModelPrice = {
      model: Model.MERCEDES,
      price: 20
    const citroen: CarModelPrice = {
      model: Model.CITROEN,
      price: 15
    const suzuki: CarModelPrice = {
      model: Model.SUZUKI,
      price: 30

    let carModelPriceArr : CarModelPrice[] = [ sedan, mercedes, citroen, suzuki ];


up on sorting carModelPriceArr i would expect the following result.


How can i do the custom sorting on the array carModelPriceArr?

following is the typescript playground for this

appreciate if you can help thank you


You could define a priority level as a number for every model, then sort like this:

const cars = [{"model":"SEDAN","price":25},{"model":"MERCEDES","price":20},{"model":"CITROEN","price":15},{"model":"SUZUKI","price":30}];

const priority = {
  SEDAN: 1,
  SUZUKI: 3,

const result = cars.sort(({ model: modelA }, { model: modelB }) => priority[modelA] - priority[modelB]);


Leave a Reply

Your email address will not be published. Required fields are marked *