Sort JavaScript array with mixed characters

Error processing SSI file

Answers

  1. Esposito

    • 2018/2/26

    Your sort is a bit tricky since the _FORM keeps it from being just a straightforward lexicographical sort.

    Try this:

    var SortedArray = array.sort(function(a, b){
                        a = a.slice(0, -5);
                        b = b.slice(0, -5); 
                        return a < b ? -1 : (a > b) ? 1 : 0;
                      });
    
  2. Gregory

    • 2018/1/6

    Arrays in JavaScript come with a built-in function that is​ used to sort elements in alphabetical order. However, this function does not directly work on arrays of numbers or objects. Sorting techniques. Using a custom sort function. Using a custom, dynamic sort function.

  3. Keenan

    • 2015/10/7

    The sort () method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort () method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

  4. Yisroel

    • 2015/4/18

    function alphabeticalOrder(arr) { return arr.sort. // Nothing after that return line does anything // JS doesn't need to look at it because the // 

  5. Stone

    • 2020/3/30

    As SHIELDHEAD suggested, you can pass a custom comparator function into Array.sort() when you want to sort by different rules than the default alphabetical/ordinal rules.

    The format of the comparator function is as follows:

    function(a,b){
         // if a should be before b, return -1
         // if b should be before a, return 1
         // if they are equal, return 0
         return a < b ? -1 : a > b ? 1 : 0;
    }
    

    In your case, I believe what your comparator function will need to do is grab the substring between "S" and "F" in your strings and compare those.

    You can get that substring using regex: a = a.match(/(?!S)([0123456789_])+(?!F)/g);

    Here's the working code:

    var array = new Array("S1_FORM", "S2_FORM", "S3_2_FORM", "S3_FORM", "S3_3_FORM", "S4_FORM");
    array.sort(function(a,b){
    	a = a.match(/(?!S)([0123456789_])+(?!F)/g);
    	b = b.match(/(?!S)([0123456789_])+(?!F)/g);
    	return a < b ? -1 : a === b ? 0 : 1;
    });
    document.getElementById("output").innerHTML = JSON.stringify(array);
    <div id="output"/>
  6. Noah

    • 2016/11/8

    Code language: JavaScript (javascript) To sort an array of numbers numerically, you need to pass into a custom comparison function that compares two numbers. The following example sorts the scores array numerically in ascending order.

  7. Harris

    • 2015/12/6

    Get code examples like "javascript sort array of objects by key value" instantly right from your google search results with the Grepper Chrome Extension.

  8. Bianchi

    • 2020/7/20

    Sort an array of objects by string property value in JavaScript and TypeScript. Atit. Follow. Nov 11, Let’s discuss one of the tricks to sort the array and object by property.

  9. David

    • 2020/2/14

    I believe you want a custom sort comparison function. See this post: How to define custom sort function in javascript?

  10. Parker

    • 2017/12/4

    The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

  11. Isaac

    • 2016/9/5

    The sort () method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort () method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

  12. Eric

    • 2018/8/18

    underscore.js. use underscore, its small and awesome sortBy_.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the 

  13. Kieran

    • 2017/4/6

    Use Array.sort() to sort an array; Clone array using spread operator (…) to make the function pure; Sort by desired key (updated_at) Convert date string to date object; Array.sort() works by subtracting two properties from current and next item if it is a number / object on which you can perform arrhythmic operations

  14. Armando

    • 2020/9/27

    Let's discuss one of the tricks to sort the array and object by property. There are 5 different ways we can achieve the following functionality 

  15. William

    • 2016/3/11

    In our example above, we want to be able to sort an array of objects, whose values are either strings or numbers. If, however, you know that you’ll only be dealing with objects whose values are

  16. Finnegan

    • 2017/12/29

    The sort() method can be used to sort the array based on the values of one of these properties, such as sorting the array by name, age, or even when they retire.

  17. Joey

    • 2021/5/1

    How would you sort this array with these objects by distance. So that you have the objects sorted from smallest distance to biggest distance ? Object { distance=3388, duration="6 mins", from="Lenc

  18. Sanchez

    • 2015/7/27

    Suppose you have an array users . You may use users.sort and pass a function that takes two arguments and compare them (comparator).

  19. Pablo

    • 2021/4/28

    Approach : Comparing the ASCII code to sort the string letters using JavaScript For sorting string letters in alphabetical order, first you'll split the string into an array. Then you need to iterate the array and compare each element with the rest of the other elements on the array.

  20. Sergio

    • 2017/8/19

    I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript. How can I do it? var user = { bio: null, email: " 

  21. Jose

    • 2018/5/20

    To sort an array of objects by some key alphabetically in descending order, you only need to add as prefix a - (minus) symbol at the beginning of the key string, so the sort function will sort in descending order:

  22. Stetson

    • 2017/3/3

    How do I sort an array of objects in TypeScript?

  23. James

    • 2019/6/15

    For sorting a array you must define a comparator function. This function always be different on your desired sorting pattern or order(i.e. ascending or descending). Let create some functions that sort an array ascending or descending and that contains object or string or numeric values.

  24. Ricky

    • 2020/4/17

    In this tutorial, you will learn how to sort an array of objects by the values of the object's properties.

  25. Kylan

    • 2018/1/9

    Now we need to sort the objects in an above array alphabetically by using its name property. JavaScript has a built-in sort () method which helps us to sort an array alphabetically.

  26. Kaiden

    • 2018/9/27

    JavaScript Array sort() Method​​ The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order.

  27. Wyatt

    • 2017/3/17

    Just can't think of many cases of sorting items alphabetically including casing, Sort JavaScript array of Objects based on one of the object's properties. 15.

  28. Martin

    • 2016/10/4

    By default, the sort() method sorts the array elements in ascending order with the let numbers = [0, 1 , 2, 3, 10, 20, 30 ]; numbers.sort( function( a , b){ if(a > b) you use the sort() method without passing the compare function as shown in the 

  29. Walker

    • 2019/4/7

    The sort () method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort () method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

  30. Yahir

    • 2021/7/26

    The employees array above is an array of objects with properties of different data types, from string, numeric, to a date string. The sort() method can be used to 

  31. Alec

    • 2018/6/22

    When the sort () function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. If the result is negative a is sorted before b. If the result is positive b is sorted before a. If the result is 0 no changes are done with the sort order of the two values.

  32. Armando

    • 2017/8/29

    Sorting an array of objects in JavaScript can be a nightmare if you don't by some key in ascending or descending order using plain JavaScript (no extra comes before, after or is the same as string2 in sort order (values).

  33. Beckham

    • 2020/4/21

    sortBy_.sortBy (list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator. Iterator may also be the string name of the property to sort by (eg. length).

  34. Sebastian

    • 2016/7/26

    Sorting Object Arrays. JavaScript arrays often contain objects: Example. var cars = [ {type:" 

  35. Stone

    • 2019/4/3

    Introduction to JavaScript Array sort () method The sort () method allows you to sort elements of an array in place. Besides returning the sorted array, the sort () method changes the positions of the elements in the original array.

  36. Jaxon

    • 2016/5/20

    Cem Eygi. JavaScript Array Sort – How to Use JS Sort Methods (With Code Examples). In JavaScript, we can 

  37. Frasheri

    • 2015/4/14

    To sort an array of objects, you use the sort () method and provide a comparison function that determines the order of objects. Suppose that you have an array of employee objects as follows:

  38. Palmieri

    • 2017/8/18

    I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: '​Jamf' }, 

  39. Jackson

    • 2020/9/22

    Javascript array sort () method provides an easy way to sort the array elements. We can sort simple elements like string, integer or any complex objects like we have seen above. Try to run the example shown above and drop one comment below if you have any queries. Where is the color and why codevscolor ?

  40. Shepherd

    • 2016/10/1

    In this article, I'll show you how to sort an array of objects in JavaScript with no This function has two parameters — the key we want to sort by and the order of 

  41. Matthias

    • 2016/5/14

    Array.sort () should receave a return value of one of 3 possible outcomes: A positive number (first item > second item) A negative number (first item < second item) 0 if the two items are equal

  42. Zachariah

    • 2020/10/28

    The employees array above is an array of objects with properties of different data types, from string, numeric, to a date string. The sort() method can be used to 

  43. Raphael

    • 2021/8/28

    Introduction to JavaScript Array sort () method The sort () method allows you to sort elements of an array in place. Besides returning the sorted array, the sort () method changes the positions of the elements in the original array.

Comments are closed.

More Posts