peterschussheim
10/13/2016 - 2:21 PM

sort

sort

const lessons = [
    {
        title: 'Javascript Arrays in Depth - join',
        views: 960,
        tags: ['array', 'join']
    },
    {
        title: 'Javascript Arrays in Depth - concat',
        views: 1050,
        tags: ['array', 'concat']
    },
    {
        title: 'Javascript Arrays in Depth - slice',
        views: 2503,
        tags: ['array', 'slice']
    },
    {
        title: 'Javascript Functions in Depth - bind',
        views: 2500,
        tags: ['functions', 'bind']
    }
];

var list = lessons
	.sort((a, b) => b.views - a.views)
	.map(x => `	<li>${x.title} (${x.views})</li>`)
	.join('\n');

var output = `<ul>\n${list}\n</ul>`;
///Sort elements IN-PLACE and returns the original array.
var items = [10, 20, 30, 1, -5]; ///\`sort\` converts each integer to a string.
items.sort(); ///Result may be undesireable since the order of unicode chars is used.


///A workaround to the type coercion is to use a comparison function that uses the raw values in the array.


var ascCompare = (a, b) => a - b; ///Only cares whetrher return value is:\\- a - b === 0//- a - b < 0//- a - b > 0
var ascendingSort = items.sort(compare);

sort

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.