logo
down
shadow

AngularJS or Javascript: Sort array(ASC & DESC) using index only


AngularJS or Javascript: Sort array(ASC & DESC) using index only

Content Index :

AngularJS or Javascript: Sort array(ASC & DESC) using index only
Tag : javascript , By : rhinojosa
Date : November 29 2020, 12:01 PM

I think the issue was by ths following , First - arrObject is an array of objects - this allows an index in the first place.
This issue seems to be a js only issue - simply use unshift() to add new items to the start of the array
var arrObject = [
{id:'05', name:'Harrison'},
{id:'04', name:'Nick'},
{id:'03', name:'Tom'},
{id:'01', name:'John'}
];

arrObject.unshift({id:'06', name:'Gavin'});

console.log(arrObject); // puts the newly added Gavin object ar ethe start of the array.
var arrObject = [
{id:'01', name:'John'},
{id:'03', name:'Tom'},
{id:'04', name:'Nick'},
{id:'05', name:'Harrison'},
];

arrObject.push({id:'06', name:'Gavin'});// puts the Gavin object ar ethe end of the array.

console.log(arrObject.reverse()); // renders the array in reverse orderwith the Gavin object shown first

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Mysql sort and sort again, but this does not work ORDER BY date DESC, view DESC


Tag : mysql , By : JSebok
Date : March 29 2020, 07:55 AM
this one helps. I've got a table with 400 entry, I do a first sort to get the latest entry and limit 20 , This should work; you just select from the first result set.
SELECT x.date, x.view FROM (SELECT date,view FROM `blog` order by date DESC limit 20) as x order by view DESC

MongoDB updating nested array with sort and slice, doesn't sort desc


Tag : javascript , By : mgaspar
Date : March 29 2020, 07:55 AM
I wish did fix the issue. What is happening is that you have asked MongoDB to sort the items in descending order and keep only the last 5; this means that only the 5 smallest items will be kept. But since you are inserting the items in ascending order (because Date() keeps increasing), once you have inserted 5 items you already have the 5 smallest items, so no subsequent ones will be inserted.
As you have noted if you change the sort order to ascending then you will be keeping the 5 largest items; since every new item has a larger Date() than all the previous ones, every new item will initially be among the largest 5 items so will appear in the list.

Javascript sort dates in array by year DESC, then month ASC, then day ASC


Tag : javascript , By : KS9
Date : March 29 2020, 07:55 AM
wish helps you Yo can split the data and sort it independently by year desc, month asc and day asc.
var data = [{ "course": "First Aid", "courseDate": "2016-04-25T00:00:00-06:00" }, { "course": "CPR", "courseDate": "2016-04-06T00:00:00-06:00" }, { "course": "ASL1", "courseDate": "2016-01-07T00:00:00-06:00" }, { "course": "ASL2", "courseDate": "2016-03-25T00:00:00-06:00" }, { "course": "ASL2X", "courseDate": "2015-03-25T00:00:00-06:00" }];

data.sort(function (a, b) {
    var aa = a.courseDate.split(/\D/),
        bb = b.courseDate.split(/\D/);
    return bb[0] - aa[0] || aa[1] - bb[1] || aa[2] - bb[2];
});

document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

How to sort a multi dimension PHP array with a key DESC wise without rearranging its index (can remove those elements)


Tag : php , By : Philofax
Date : March 29 2020, 07:55 AM
like below fixes the issue This will work for you for sure. Check out the below methods.
array_column array_unique array_multisort
array_multisort(array_column($array, 'directors_count'), SORT_DESC,
                array_column($array, 'user_id'),      SORT_DESC,
                $array);

$tempArr = array_unique(array_column($array, 'directors_count'));
$final_array = array_intersect_key($array, $tempArr);                
print_r($final_array);
Array ( 
 [0] => Array ( [user_id] => 10 [directors_count] => 6 ) 
 [1] => Array ( [user_id] => 12 [directors_count] => 5 ) 
 [2] => Array ( [user_id] => 15 [directors_count] => 2 ) 
 [4] => Array ( [user_id] => 16 [directors_count] => 1 ) 
 [7] => Array ( [user_id] => 17 [directors_count] => 0 ) 
)

In JavaScript how do I sort an array of arrays by asc / desc


Tag : javascript , By : Kenny
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You can create a function which takes Array, column no and the sort order(asc/desc).
You can use a trick to use a same function for asc/desc sort. Have a variable whose value will be 1 or -1. Always multiply it with each result or sort() callback. If you will pass 1 it will sort asc and desc for -1
const tableRows = [
  ['zz', 'cc'],
  ['aa', 'cc'],
  ['uu', 'dd']
];

function sortBy(arr, col, type = 1) {
  return [...arr].sort((a, b) => a[col].localeCompare(b[col]) * type)
}

console.log(sortBy(tableRows, 1)) //asc
console.log(sortBy(tableRows, 0, -1)) //desc
Related Posts Related QUESTIONS :
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • Rendering RSS feed (XML) in React with jquery, why do object values parsed from the feed seem empty?
  • Node.js xml2js http.request tag matching
  • applying texture to custom plane geometry in Threejs
  • How do I pass an Event as a function parameter in HTML written inside JavaScript variable?
  • Interface of object parameter with default value and default property
  • Does using try / catch rather than `.catch` observable operator with XHR requests reduce performance?
  • In IE11 background colors not working on printing
  • TypeScript interface signature "(): string"
  • Is synchronous XMLHttpRequest supported in Internet Explorer 10 and 11?
  • Axios function not returning any values with js
  • How to use firestore TIMESTAMP to create time-stamp in JavaScript
  • Instantiate subclass without constructing
  • Outputing a created object in javascript to a server side JSON file
  • Transition the numeric labels in a bar chart
  • createBottomTabNavigator: hide just one tab from the tabBar
  • iife vs simple statement behavior with document.title
  • showDialogPopup Refresh Page after Click
  • Child component does not set the initial value passed from the parent: ReactJS
  • Nodejs javascript added to button not responding
  • Trying to define a promise.all
  • Python GEE to extract featurecollection timeseries from Landsat imageries
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • Is there value in propTypeing repeat required properties at every level of nested components?
  • Call function if variable does not exist in a filter
  • localStorage value doesn't get updated automatically
  • React not updating state with setState correctly inside promises
  • Direct native JavaScript or jQuery method to get containing block of an element
  • Dynamic JSON object to Html Table
  • How to send Id and file to same controller?
  • How to Make a jQuery Table Cell(td) clickable to Run a Function?
  • Prevent the duplicate function onchange event jQuery
  • Reactjs Input Upload doesn't return local URL
  • An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected
  • How to filter a JSON Array, with an eventListener input value at keyup
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com