0

This is not a duplicate because I want to select the order based on the chosen category by the user.

Trying to sort an array of objects by category array field i.e sort by user selected category from maybe a selected list.

Any help would be much appreciated.

var users = [
  { 'user': 'barne1y', 'age': 34, 'category':['a','b','c','d','e']},
  { 'user': 'fred',   'age': 40, 'category':['a','b','c','d']},
  { 'user': 'fred',   'age': 48, 'category':['a','b'] },
  { 'user': 'barney', 'age': 36, 'category':['a','b','c']}
];

example: I have a list of users that can be appear in multiple categories as shown in the json data above then in the UI I have a selected list with all the possible categories e.g.

<select><option>a</option><option>b</option><option>c</option><option>d</option></select>

When an option e.g a is selected the data is then sorted using that value found in the category.

4
  • plenty of typos in your example Commented Feb 7, 2017 at 1:28
  • Post an example of the expected result! Commented Feb 7, 2017 at 1:29
  • where is grade property? Commented Feb 7, 2017 at 1:31
  • 1
    Are you sure you want the value of category to be an array whose single value is a comma-delimited string? In other words, do you not mean ['a', 'b']? Are these values to be involved in the sort? Commented Feb 7, 2017 at 1:34

2 Answers 2

0

You can sort this collection by using Array.prototype.sort() and passing in a compare function:

var users = [
  { 'user': 'barne1y', 'age': 34, 'category':['a,b,c,d,e']},
  { 'user': 'fred',   'age': 40, 'category':['a,b,c,d']},
  { 'user': 'fred',   'age': 48, 'category':['a,b'] },
  { 'user': 'barney', 'age': 36, 'category':['a,b,c']}
]; 

var sortedUsers = users.sort(sortByAge);

function sortByAge(a, b) {
  return a.age - b.age;
}
Sign up to request clarification or add additional context in comments.

2 Comments

that was a typo @hackerrdave I have since corrected it.
if it was sort by age it would be a duplicate but i wanted to sort by category value coming from the user
0

var users = [
  { 'user': 'barne1y', 'age': 34, 'category':['a','b','c','d','e']},
  { 'user': 'fred',   'age': 40, 'category':['a','b','c','d']},
  { 'user': 'fred',   'age': 48, 'category':['a','b'] },
  { 'user': 'barney', 'age': 36, 'category':['a','b','c']}
];

users.sort(function(a, b) {
  return a.category.length - b.category.length; // reverse the order if you want descending sort
});

console.log(users);

6 Comments

thanks @ibrahim mahrir for your solution but the problem I had was that I wanted to want to sort via a value selected from a dropdown list.
@kdev check the link for the duplicate. It has what you need!
its not a duplicate since want to sort by category value coming from the user
@kdev Post some example. The question still a little bit foggy!
@kdev do you mean filter? (i.e show only the users that have that category)?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.