Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reverse sort array of objects

const books = [
  {id: 1, name: 'The Lord of the Rings'},
  {id: 2, name: 'A Tale of Two Cities'},
  {id: 3, name: 'Don Quixote'},
  {id: 4, name: 'The Hobbit'}
]
// the 1 and -1 can be switched for standard sort
books.sort((a,b) => (a.id > b.id) ? -1 : ((b.id > a.id) ? 1 : 0));
// Code idea from fancyfinch
Comment

Sort and Reverse an array of Objects using JavaScript

<!DOCTYPE html>
<html>
<body>
<p>The reverse() method reverses the elements in an array.</p>
<p>By combining sort() and reverse() you can sort an array in descending order.</p>
<button onclick=”sortAndReverseArrayValue()”>Click</button>
<p id=”pId”></p>
<script>
var banksOfIndis = [“CentralBankOfIndia”,”AndhraBank”,”BankOfBaroda”,”CanaraBank”,”AllhabadBank”];
document.getElementById(“pId”).innerHTML = banksOfIndis;
function sortAndReverseArrayValue() {
banksOfIndis.sort();
banksOfIndis.reverse();
document.getElementById(“pId”).innerHTML = banksOfIndis;
}
</script>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: bcrypt 
Javascript :: js append to array 
Javascript :: ** javascript Exponentiation 
Javascript :: base64 encode in javascript 
Javascript :: react eslint prettier 
Javascript :: c# beautify json string 
Javascript :: javascript date get next 15 minutes 
Javascript :: how to do get request in axios 
Javascript :: javascript how to get last property of object 
Javascript :: yup string date schema validation 
Javascript :: try catch async await 
Javascript :: remove spaces from string javascript 
Javascript :: acheck angular version 
Javascript :: google map react search place 
Javascript :: recieve voice channel audio discord.js 
Javascript :: convert curl response to json format and echo the data 
Javascript :: document.getelementbyid 
Javascript :: Comparing and Filtering two arrays 
Javascript :: post request with data and headers 
Javascript :: js random unique id 
Javascript :: try and catch express 
Javascript :: js dom siblings 
Javascript :: jquery remove multiple class 
Javascript :: javascript get magnitude of number 
Javascript :: javascript some 
Javascript :: put new attribute on html tag using javascript 
Javascript :: replace character inside a string in JavaScript 
Javascript :: graphql query 
Javascript :: moment to date object 
Javascript :: funciones invocan a funciones javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =