Sort Steam Achievement

Makes Steam achievements sorted by unlock date.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Sort Steam Achievement
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Makes Steam achievements sorted by unlock date.
// @author       Makazeu
// @match        *://steamcommunity.com/id/*/stats/*
// @grant        none
// @require      https://cdn.bootcss.com/moment.js/2.18.1/moment.min.js
// ==/UserScript==

/* jshint esversion: 6 */

(function() {
    'use strict';

    let comparator = (a, b) => {
        let a_datetime = datetimeParser(
            a.children[1].children[0].children[0].innerText.trim());
        let b_datetime = datetimeParser(
            b.children[1].children[0].children[0].innerText.trim());
        return a_datetime.isBefore(b_datetime) ? -1 : 1;
    };

    let datetimeParser =  datetime => {
        datetime = datetime.substring('解锁日期 '.length);
    
        datetime = datetime.replace('上午', ' AM ');
        datetime = datetime.replace('下午', ' PM ');
    
        if (datetime.includes('年')) {
            return moment(datetime, 'YYYY年MM月DD日 A HH:mm');
        } else {
            return moment(datetime, 'MM月DD日 A HH:mm');
        }
    };

    let achieveFilter = (key, value) => {
        if (value.innerHTML === '') return false; 
        if (value.children[1].children[0].children.length < 3) return false;
        if (!value.children[1].children[0].children[0]
            .innerText.includes('解锁')) return false;
        return true;
    };

    let achieveSet = jQuery('#personalAchieve');
    let achieveRows = achieveSet.children().filter(achieveFilter);
    let arr = Array.from(achieveRows);
    
    arr.sort(comparator);

    for (let i = 0; i < achieveRows.length; i++) {
        let element = achieveRows[i];
        element.remove();
    }

    arr.forEach( achieve => {
        //console.log(achieve.children[1].children[0].children[1].innerText);
        achieveSet.prepend(achieve);
    });
    
})();