admin管理员组

文章数量:1400455

There are lots of examples about sorting but i couldn't understand anyone. Can anyone help to toggle sort when the button is clicked.

<button onclick="sortName()">Sort</button>

var contacts= [
{
  "id": 1,
  "name": "xyz",
  "email": "[email protected]",
  "phone": "0000958331"
},
{
  "id": 2,
  "name": "abc",
  "email": "[email protected]",
  "phone": "0110958332"
},
{
  "id": 3,
  "name": "efg",
  "email": "[email protected]",
  "phone": "0220958333"
}
]

function sortName(){
 contacts.sort(function (a, b) {
 const x = a.name.toLowerCase();
 const y = b.name.toLowerCase();
 return (x < y ? -1 : x > y ? 1 : 0);
});

console.log(contacts);
}

Link:

There are lots of examples about sorting but i couldn't understand anyone. Can anyone help to toggle sort when the button is clicked.

<button onclick="sortName()">Sort</button>

var contacts= [
{
  "id": 1,
  "name": "xyz",
  "email": "[email protected]",
  "phone": "0000958331"
},
{
  "id": 2,
  "name": "abc",
  "email": "[email protected]",
  "phone": "0110958332"
},
{
  "id": 3,
  "name": "efg",
  "email": "[email protected]",
  "phone": "0220958333"
}
]

function sortName(){
 contacts.sort(function (a, b) {
 const x = a.name.toLowerCase();
 const y = b.name.toLowerCase();
 return (x < y ? -1 : x > y ? 1 : 0);
});

console.log(contacts);
}

Link:https://codepen.io/sarash/pen/PjpLdd?editors=1010

Share Improve this question asked Jun 20, 2017 at 19:40 srssrs 351 silver badge5 bronze badges 5
  • This code works but i dont know how to make it toggle so when the button is clicked 2nd time the name should appear in descending order and so on. – srs Commented Jun 20, 2017 at 19:46
  • You might look at the localeCompare method to pare two strings, developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – steve-kasica Commented Jun 20, 2017 at 19:47
  • If i could have understand so easily just googling it then why should i have to post it here – srs Commented Jun 20, 2017 at 19:50
  • There is nothing actually doing the sorting. Your return statement is just paring the values of x and y and returning a 1 or a -1, but you're not passing anything to sort. Additionally, you would need to take action based on that parison. There are literally dozens of ways to do this, for instance khan4019.github.io/front-end-Interview-Questions/sort.html. – Raydot Commented Jun 20, 2017 at 19:50
  • Actually i am not showing any errors in this code i just wanted an idea that how do i make that button toggle so that the name will appear ascending to descending and vice versa. – srs Commented Jun 20, 2017 at 19:57
Add a ment  | 

1 Answer 1

Reset to default 5

To make it toggle you can put a variable outside the called function to store current order and toggle it on every click:

var order = false;
var contacts= [
{
  "id": 1,
  "name": "xyz",
  "email": "[email protected]",
  "phone": "0000958331"
},
{
  "id": 2,
  "name": "abc",
  "email": "[email protected]",
  "phone": "0110958332"
},
{
  "id": 3,
  "name": "efg",
  "email": "[email protected]",
  "phone": "0220958333"
}
]

function sortName(){
  order = !order;
  contacts.sort(function (a, b) {
    const x = a.name.toLowerCase();
    const y = b.name.toLowerCase();
    return (order ? x > y : x < y);
  });
  console.log(contacts);
}

本文标签: how to toggle sort in javascriptStack Overflow