logo
down
shadow

Sorting an array based on first digit of property


Sorting an array based on first digit of property

By : Dsx
Date : November 17 2020, 11:55 AM
it fixes the issue You can your use regex within Javascript's sort function, like the following.
code :
var model = [
    {
        "title": "test3"
    },
    {
        "title": "test1"
    },
    {
        "title": "test2"
    },
    {
        "title": "test0"
    }
];
model.sort(function (a, b) {
    //Strips out alpha characters
    a = a.title.replace(/\D/g, '');
    b = b.title.replace(/\D/g, '');

    //sets value of a/b to the first zero, if value beings with zero.
    //otherwise, use the whole integer.
    a = a[0] == '0' ? +a[0] : +a;
    b = b[0] == '0' ? +b[0] : +b;

    return a - b;
});


Share : facebook icon twitter icon
Sorting only one property inside an array based on another property

Sorting only one property inside an array based on another property


By : Christine
Date : March 29 2020, 07:55 AM
will help you
I came upon a solution which looks pretty bad, it involves 2 extra copies of deepCloned arrays
code :
arr.slice() // create new array with the same objects
 .sort(function(a,b){return a.id-b.id;}) // sort that (leave original untouched)
 .forEach(function(o,i){o.sortedPosition = i;}); // update the objects
Sorting an object array based on a property

Sorting an object array based on a property


By : Renato Ferreira
Date : March 29 2020, 07:55 AM
Does that help EDIT: Added some information. , It looks like reverse order
code :
public int compareTo(Unit compareUnit) {
    if (getValue(0) < compareUnit.getValue(0)) return 1;
    else if (getValue(0) > compareUnit.getValue(0)) return -1;
    return 0;
}
Sorting an array of objects based on a property value (int)

Sorting an array of objects based on a property value (int)


By : Houcer
Date : March 29 2020, 07:55 AM
like below fixes the issue This question deals with my algorithm and why it isn't working. More specifically, I would like to know how it can be improved to do what I want it to do. That is why it is different from the suggested duplicate question. , You can use Array.prototype.sort() and define a compare function:
code :
function compareIndexFound(a, b) {
  if (a.indexFound < b.indexFound) { return -1; }
  if (a.indexFound > b.indexFound) { return 1; }
  return 0;
}

list.sort(compareIndexFound);
function compareIndexFound(a, b) {
  return a.indexFound - b.indexFound;
}
list.sort((a, b) => a.indexFound - b.indexFound);
function sortBy(arr, prop) {
  return arr.sort((a, b) => a[prop] - b[prop]);
}

sortBy(list, 'indexFound');
Sorting Array based on property of object on inner array

Sorting Array based on property of object on inner array


By : Vivin
Date : March 29 2020, 07:55 AM
To fix this issue I've been asked to provide a full answer, here we go:
First, I would like to draw your attention to a "design hole" that you have: what happens if you don't have this "person" at all? for this case, I've allowed myself to assume that all those results would be shown at the end.
code :
IEnumerable<Account> sortedAccounts = accounts
            .Select(x => {
                x.PersonRoles = x
                .PersonRoles
                .OrderBy(acct => {
                    if (acct.PersonNumber != 001)
                        return 999;
                    return acct.RoleOrder;
                }).ToArray();
                return x;
            })
            .OrderBy(acct => {                
                var relevantRoles = acct.PersonRoles
                    .Where(role => role.PersonNumber == 0001)
                    .Select(x => x.RoleOrder);
                if (relevantRoles.Any() == false)
                    return 999;
                return relevantRoles.Max();
                }
            ).ToList();
Sorting an array based on property value of another array of objects

Sorting an array based on property value of another array of objects


By : user3056971
Date : March 29 2020, 07:55 AM
To fix this issue You can use .reduce() to change the list array to object and then sort based on this object.
Demo:
code :
var list = [
       { name: "A", distance: 1},
       { name: "B", distance: 2},
       { name: "C", distance: 3},
       { name: "D", distance: 4},
       { name: "E", distance: 5},
       { name: "F", distance: 6},
       { name: "G", distance: 7},
       { name: "H", distance: 8} 
    ];
    
var disturbed = ["G", "B", "C", "F"];
    
var sort = list.reduce((acc, cur) => {
  acc[cur.name] = cur.distance;
  return acc;
}, {});

disturbed.sort((a, b) => sort[a] - sort[b]);

console.log(disturbed)
Related Posts Related Posts :
  • Build Fails: `npm rebuild node-sass --force`
  • Angular Datepicker change dateformat
  • Trying to extract data from between two sets of characters
  • Configuring Jest in WebStorm
  • Uncaught Error: Syntax error, unrecognized expression: tr.ec_portfolio_title,
  • JQuery fading in fading out continuously
  • Unexpected end of input (Line 1) and Undefined Function?
  • How to animate the width of a div slowly with jquery
  • JavaScript + RegEx Complications- Searching Strings Not Containing SubString
  • SignalR - Unable to get property 'client' of undefined or null reference
  • full calendar not displaying when loaded (with backbone)
  • Angular: Update service and share data between controllers
  • ReplaceAll Google script
  • Search for matching LI items in separate UL
  • Access arguments of an... argument
  • Customizing Google custom search jsapi (query strings)
  • React | Can´t load Images > Module not found
  • Dynamically creating buttons in DOJO
  • 404 Not Found in AJAX post call
  • How do I find out, using javascript, what software opened and running my application?
  • Javascript to check the status of wireless connection
  • StarDict support for JavaScript and a Firefox OS App
  • Phonegap event Resume
  • call javascript function on ENTER key press?
  • When is the best time to remove no-js classes from the html tag
  • Recommended email sending languages
  • setInterval not working properly with chrome
  • Does IE create new scope for each script tag?
  • Uncaught SyntaxError: Unexpected token < Underscore and parse issues
  • How to pass captured URL to Email Body in HTML
  • How to structure default nested resources?
  • Store all the keys of a JSON object in a variable
  • Need an efficient way to group the Array of object in javascript
  • a sensible approach to highcharts x-axis labels
  • IDE autocompletion for javascript AMD loading style
  • Bootstrap menu disappears after first click setting the ul style to none
  • split ajax json response errors in each field
  • how to add javascript in head in cmsms
  • REGEX: Finding the correct occurrence order of some given special characters in a string
  • How do you preserve a JavaScript date's time zone from browser to server, and back?
  • Return binary result from phantomjs webserver
  • must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
  • Replacing a substring of an element's text with another string
  • How do I filter marks greater than or equal to 90 in this array?
  • Modify the text of my radio input button?
  • Clicking on status bar does not scroll to top of app (Trigger.io)
  • Mocha JS: How to highlight specific assertion failure?
  • $.getJSON jquery parsing to HTML
  • Find items of a certain class (a) that are also in other classes (b,c,d)
  • Setting different images for D3 force-directed layout nodes
  • Integrating Dropzone.js with angular
  • Control sprite animation speed
  • Can only one error / success function be assigned for AngularJS $http promises?
  • Working with math in javascript
  • Django variable isnt recognized by javascript
  • $.when does not work with $.scrollTo plugin
  • How to read local xml file using $.ajax?
  • Manifest is not Valid JSON. What is wrong?
  • AngularJS re-initialise JS after statechange
  • WebRupee is not working
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org