JavaScript How can I partially sort an array based on some condition? sort partially sorted array,c++ partial sort an array,best

How can I partially sort an array in JavaScript, based on some conditions that I choose?

Given an array

var tab = [
    {html: 'This is a test'},
    {html: 'Locked item', locked: true},
    {html: 'Another item'},
    {html: 'Another one'}
    //and 100 more items...
];

The 2nd items is called "locked", it should stays at the same place (2nd place) after the array is sorted

var tab = [
    {html: 'Another item'},
    {html: 'Locked item', locked: true},
    {html: 'Another one'},
    {html: 'This is a test'},
    //and 100 more items...
];

This is the complete code I currently have:

var tab = [
    {html: 'This is a test'},
    {html: 'Locked item', locked: true},
    {html: 'Another item'},
    {html: 'Another one'}
    //and more items...
];

tab.sort(function(a,b){
    var aVal = a.html.toLowerCase();
    var bVal = b.html.toLowerCase();
    if (aVal===bVal)
        return 0;
    else
        return aVal < bVal ? -1 : 1;

    //how to handle locked items?
});

//desire output
/*var tab = [
    {html: 'Another item'},
    {html: 'Locked item', locked: true},
    {html: 'Another one'},
    {html: 'This is a test'},
    //and more items...
];*/

console.log(tab);

See fiddle

Answer:1

Probably could be optimised, but removing all locked items and storing their initial positions, then adding them back into the sorted array should work:

var tab = [
    {html: 'This is a test'},
    {html: 'Locked item', locked: true},
    {html: 'Another item'},
    {html: 'Another one'}
    //and more items...
];

var stored = {}, newTab = [];
for(var i = 0, iLimit = tab.length; i < iLimit; i++) {
    if(tab[i].locked) {
        stored[i] = tab[i];
    } else {
        newTab.push(tab[i]);
    }
}

newTab.sort(function(a,b){
    var aVal = a.html.toLowerCase();
    var bVal = b.html.toLowerCase();
    if (aVal===bVal) {
        return 0;
    } else {
        return aVal < bVal ? -1 : 1;
    }
});

for(var indice in stored) {
    newTab.splice(indice, 0, stored[indice]);
}

console.log(newTab);

http://jsfiddle.net/qW7uH/2/

Answer:2

I am new to google api. I am able to do this file upload from app script and any file which is uploaded through that script get stored to my drive only. But how to do this using javascript. Example ...

I am new to google api. I am able to do this file upload from app script and any file which is uploaded through that script get stored to my drive only. But how to do this using javascript. Example ...

  1. upload file google drive
  2. upload file google colab
  3. upload file google form
  4. upload file google drive api
  5. upload file google cloud storage
  6. upload file google drive python
  7. upload file google doc
  8. upload file google app script
  9. upload file google storage
  10. upload file google drive php
  11. upload file google sheets
  12. upload file google drive c#
  13. upload file google sites
  14. upload file google cloud storage nodejs
  15. upload file google drive java
  16. upload file google drive android
  17. upload file google drive mobile
  18. upload file google translate

I have a large JSON file with a snippet of the file below. Please ignore the values as they are only used as placeholders for this question. "restaurants":[ "name": "Burger King", ...

I have a large JSON file with a snippet of the file below. Please ignore the values as they are only used as placeholders for this question. "restaurants":[ "name": "Burger King", ...

  1. jquery iterate through array
  2. jquery iterate through elements
  3. jquery iterate through table rows
  4. jquery iterate through json
  5. jquery iterate through child elements
  6. jquery iterate through select options
  7. jquery iterate through json array
  8. jquery iterate through array of objects
  9. jquery iterate through list
  10. jquery iterate through checkboxes
  11. jquery iterate through elements with same class
  12. jquery iterate through json data
  13. jquery iterate through find results
  14. jquery iterate through class elements
  15. jquery iterate through table
  16. jquery iterate through table columns
  17. jquery iterate through li
  18. jquery iterate through table header
  19. jquery iterate through class items
  20. jquery iterate through all options select

I have three sliding panels in my html. The thing is that panels overlay each other when open. I would like to get a function that create an event when one panel (eg. #panel1) is open already and the ...

I have three sliding panels in my html. The thing is that panels overlay each other when open. I would like to get a function that create an event when one panel (eg. #panel1) is open already and the ...

  1. jquery sliding panels
  2. jquery sliding panel demo
  3. jquery sliding panel left to right
  4. jquery sliding panel from right
  5. jquery sliding panel from left
  6. jquery ui sliding panel
  7. jquery vertical sliding panel
  8. jquery mobile sliding panel
  9. jquery sliding sidebar panel
  10. jquery create sliding panel
  11. jquery sliding overlay panel
  12. jquery sliding menu panel
  13. jquery lateral sliding panel
  14. jquery side sliding panel plugins
  15. jquery side sliding panel

I have a Meteor method that I call from the client, which in turn updates all fields in a single document. In the last line of code below I try to immediately run the same update on the client to get ...

I have a Meteor method that I call from the client, which in turn updates all fields in a single document. In the last line of code below I try to immediately run the same update on the client to get ...

  1. meteor shower side effects