Sort a table that is created using document.write() in javascript

I have written the following code that displays a table of 4 columns on ajax success. I wish to sort this table based on the 4 columns. I tried doing it using some readymade .js files available online. but, didn’t succeed. How can I achieve the requirement?

function Success(data)
{
var obj;
obj=JSON.parse(data);
document.write("<html><head><title>Service</title></head><body 
bgcolor=#FFFFFB>");
document.write("
"); document.write("

Details

"); document.write(""); document.write(""); document.write(""); document.write(""); document.write(""); document.write(""); document.write(""); document.write("
"); for(i=0; i"); document.write(""); document.write(""); document.write(""); document.write(""); document.write(""); } document.write("
"); document.write("Name"); document.write(""); document.write("Number1"); document.write(""); document.write("Number2"); document.write(""); document.write("Number3"); document.write("
"); document.write(obj[i]); document.write(""); document.write(obj[i+1]); document.write(""); document.write(obj[i+2]); document.write(""); document.write(obj[i+3]); document.write("
"); document.write("
"); document.write("</body></html>"); } </script>


Source: stackoverflow-javascript

Sort JavaScript array by 3 values

I’d like to sort this JavaScript array by three values, but I can’t seem to figure out how to sort by more than one property at a time.

The requirements are:

  1. featured items should be at the very top
  2. Then items with no name (null)
  3. Then items with a name that are not featured
  4. Sort everything by createdAt in descending order (newest first)

This is the array:

var items [
    { name: 'foo-1', featured: true,  createdAt: 1493000001 },
    { name: null,    featured: false, createdAt: 1493000003 },
    { name: 'foo-3', featured: true,  createdAt: 1493000002 },
    { name: 'foo-4', featured: false, createdAt: 1493000004 },
    { name: 'foo-5', featured: false, createdAt: 1493000005 },
];

The result should be:

[
    { name: 'foo-3', featured: true,  createdAt: 1493000002 },
    { name: 'foo-1', featured: true,  createdAt: 1493000001 },
    { name: null,    featured: false, createdAt: 1493000003 },
    { name: 'foo-5', featured: false, createdAt: 1493000005 },
    { name: 'foo-4', featured: false, createdAt: 1493000004 },
]


Source: stackoverflow-javascript

How to sort ractive instance data in an object by its keys?

The objective is to display sorted data received from a REST API call and keep them sorted as data gets updated.

The first render seems to sort the data by itself without the need of a sorting function. But when data is updated based on results of subsequent calls using ractive.set method, I am unable to understand the strange behavior. The updated object retains the ‘index’ of the key (2012) that repeats in the updated object, even if its value (100) actually changed.

Resetting it to the original value now messes up the order in which it initially rendered the template.

Snippet has a button to simulate replacing content with new data and a button to reset to original data. How do I achieve my objective when both these changes occur. Is there a more ractive way to do this?

var firstList = {
  2014: { count: 10 },
  2013: { count: 20 },
  2012: { count: 30 },
  2011: { count: 50 }
};
var ract = new Ractive({
  template: "#template",
  el: "#output",
  data: {
    shuts: firstList,
    sort: function(obj) {
      const ordered = {};
      Object.keys(obj).sort().forEach(function(key) {
        ordered[key] = obj[key];
      });
      return ordered;
    }
  }
});

$('.change').on('click', function(e) {
  ract.set('shuts', {
    2010: { count: 1 },
    2012: { count: 100 },
    2017: { count: 17 }
  });
});

$('.reset').on('click', function(e) {
  ract.set({
    shuts: firstList
  });
});
.item .year,
.item .count {
  margin: 10px;
}
https://cdn.jsdelivr.net/ractive/0.8.12/ractive.min.js
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js


<button class="change">Change</button> <button class="reset">Reset</button>

Sorted by Year

{{#sort(shuts):year}}
{{@index}} {{year}} {{shuts[year].count}}
{{/}}


Source: stackoverflow-javascript

localeCompare() strings containing "1" and "-1" in different browsers

I’ve noticed the following differences in return values when comparing strings containing "-1", "1" and "0" using String.prototype.localeCompare() in Chrome and Edge:

"1".localeCompare("-1");
// Edge: -1, Chrome: 1

"0".localeCompare("-1")
// Edge: -1, Chrome: 1

"-1".localeCompare("1")
// Edge: 1, Chrome: -1
  1. Why is there a difference in return values between the browsers?
  2. Is there a way to have browsers behave consistently in the scenarios
    above?


Source: stackoverflow-javascript

Sort array of datesTime

I have a array of objects that have a property of date adn time, and im trying to sort by desc or asc, but it doesnt work, it gets all mix up da array.

My code:

var myDateTime = [
{date: "2016-06-08 18:10:00"},
{date: "2016-04-26 20:01:00"},
{date: "2017-02-06 14:38:00"},
{date: "2017-01-18 17:30:21"},
{date: "2017-01-18 17:24:00"},
];

var sortIt = myDateTime.sort(function(a, b) {
                    return new Date( a.date ) < new Date( b.date );
                });


Source: stackoverflow-javascript