0

I am trying to sort array of objects with name.

Working Snippet:

const data = [
   {
      "title":"Smart S",
      "tariff_id":"1301"
   },
   {
      "title":"Smart M",
      "tariff_id":"1306"
   },
   {
      "title":"Smart L",
      "tariff_id":"1303"
   },
   {
      "title":"Start",
      "tariff_id":"1304"
   },
   {
      "title":"Smart  6",
      "tariff_id":"1305"
   },
   {
      "title":"Ausland",
      "tariff_id":"888888",
   },
   {
      "title":"Länderzone",
      "tariff_id":"999999",
   }
];

//Filtering the data by removing the unwanted data
const newTariffs = (data || []).filter((tariff) =>
          tariff?.tariff_id != 888888 && tariff?.tariff_id != 999999
);

// Need to sort in this order
const tariffOrder = ["Start", "Smart S", "Smart M", "Smart L", "Smart 6"]; 

//Sort code that have been tried
const sortedTariffs = (newTariffs || []).sort((a, b) =>
    tariffOrder.indexOf(a.title) > tariffOrder.indexOf(b.title) ? 1 : -1
);


console.log("sortedTariffs ", sortedTariffs);

From the above data, the requirement is that,

-> Need to remove two items with highest id ie.., 888888 and 999999 (Used filter method)

-> Then need to sort the new filtered array based on the tariff order,

"Start", "Smart S", "Smart M", "Smart L", "Smart 6"

Current Result:

[{"title":"Smart 6","tariff_id":"1305"},{"title":"Start","tariff_id":"1304"},{"title":"Smart S","tariff_id":"1301"},{"title":"Smart M","tariff_id":"1306"},{"title":"Smart L","tariff_id":"1303"}]

Expected Result:

[{"title":"Start","tariff_id":"1304"},{"title":"Smart S","tariff_id":"1301"},{"title":"Smart M","tariff_id":"1306"},{"title":"Smart L","tariff_id":"1303"}, {"title":"Smart 6","tariff_id":"1305"}]

2 Answers 2

2

To sort by title:

const sortedTariffs = (newTariffs || []).sort((a, b) =>
    a.title.localeCompare(b.title)
);

localeCompare will compare two strings and return -1 or 1, as expected by sort.

Edit:

After reading again the question, I misread ir. The problem you have is that the string "Smart 6" has 2 spaces instead of 1, so indexOf() returns -1, and it's placed first

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

2 Comments

The above gives result as [{"title":"Smart 6","tariff_id":"1305"},{"title":"Smart L","tariff_id":"1303"},{"title":"Smart M","tariff_id":"1306"},{"title":"Smart S","tariff_id":"1301"},{"title":"Start","tariff_id":"1304"}]
@HelloWorld Sorry, I misread the question. Edited with the problem: Smart 6 has 2 spaces, so it's not found inside tariffOrder
1

You need to change the order like this to get ascending order or sort.

var sortedTariffs = newTariffs.sort((a, b) =>
    (a.title < b.title) - (a.title > b.title)
);

1 Comment

Could you please explain little more in detail how this works without providing sort order array?

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.