Use Array.prototype.sort(), eg
myArray.sort((a, b) => a.distance - b.distance)
The sort()
method accepts a comparator function. This function accepts two arguments (both presumably of the same type) and it’s job is to determine which of the two comes first.
It does this by returning an integer
- Negative (less-than zero): The first argument comes first
- Positive (greater-than zero): The second argument comes first
- Zero: The arguments are considered equal for sorting
When you’re dealing with numeric values, the simplest solution is to subtract the second value from the first which will produce an ascending order result.
Related Posts:
- What’s the difference between “{}” and “[]” while declaring a JavaScript array?
- How can I access and process nested objects, arrays or JSON?
- Sort array of objects by string property value
- What is the difference between ( for… in ) and ( for… of ) statements?
- Most efficient way to convert an HTMLCollection to an Array
- Convert Array to Object
- push object into array
- How to add an object to an array
- How to remove all duplicates from an array of objects?
- Sort array of objects by single key with date value
- Most efficient method to groupby on an array of objects
- Sorting object property by values
- How to sort an array of integers correctly
- How to sort 2 dimensional array by column value?
- Complex JSON nesting of objects and arrays
- For-each over an array in JavaScript
- Sorting an array of objects by property values
- How do I check if an array includes a value in JavaScript?
- Loop through an array in JavaScript
- How can I remove a specific item from an array?
- Find object by id in an array of JavaScript objects
- What does [object Object] mean?
- Split string into array
- How can I remove a specific item from an array?
- Array.size() vs Array.length
- Copy array by value
- What is the way of declaring an array in JavaScript?
- How to create an associative array in JavaScript literal notation
- Remove duplicate values from JS array [duplicate]
- How can I create a two dimensional array in JavaScript?
- How to loop through an array containing objects and access their properties
- Get the last item in an array
- How do I empty an array in JavaScript?
- Remove duplicate values from JS array
- How can I create a two dimensional array in JavaScript?
- How to loop through an array containing objects and access their properties
- Get the last item in an array
- How do you check if a variable is an array in JavaScript?
- How do I empty an array in JavaScript?
- How to find the sum of an array of numbers
- Remove Object from Array using JavaScript
- Short circuit Array.forEach like calling break
- Remove duplicate values from JS array
- What is the most efficient way to deep clone an object in JavaScript?
- How to check if array is empty or does not exist?
- Why is using “for…in” for array iteration a bad idea?
- Deleting array elements in JavaScript – delete vs splice
- Filter array to have unique values
- How to compare arrays in JavaScript?
- Better way to sum a property value in an array
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript
- Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop
- Merge/flatten an array of arrays
- Why is “forEach not a function” for this object?
- How to print elements from array with javascript
- Remove Object from Array using JavaScript
- Sorting strings in descending order in Javascript (Most efficiently)?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Short circuit Array.forEach like calling break
- How to remove item from array by value?
- Sorting HTML table with JavaScript
- Remove duplicate values from JS array
- How to loop through an array containing objects and access their properties
- Sort array of objects by object fields
- Nested objects in javascript, best practices
- How do I check whether an array contains a string in TypeScript?
- How do you put an image file in a json object?
- How to sort an array of objects in Java?
- How to sort an array of objects in Java?
- Converting an object to a string
- How do I check if an object has a key in JavaScript?
- Remove last item from array
- Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
- Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
- Simple average function in Javascript
- Finding the average of an array using JS
- How to change value of object which is inside an array using JavaScript or jQuery?
- Checking for duplicate strings in JavaScript array
- use Lodash to sort array of object by value
- Finding the average of an array using JS
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- Uncaught TypeError: Cannot read property ‘toUpperCase’ of undefined
- How to get the difference between two arrays in JavaScript?
- Convert array to JSON
- Return array from function
- Javascript call() & apply() vs bind()?
- How to get the first element of an array?
- element.setAttribute is not a function
- JavaScript array to CSV
- console.log showing contents of array object
- console.log showing contents of array object
- lodash multi-column sortBy
- How do I store an array in localStorage?
- How to determine if Javascript array contains an object with an attribute that equals a given value?
- Create an empty object in JavaScript with {} or new Object()?
- How to initialize an array’s length in JavaScript?
- javascript push multidimensional array
- Find a value in an array of objects in Javascript [duplicate]
- Checking if a key exists in a JavaScript object?
- Add JavaScript object to JavaScript object