Not able to sort each array of a 2D array

Viewed 61

Im trying to sort some items with two sorting layers. For example I have a list with columns. One column is the type and one is year. Now I want to sort the items type and also all items with the same type, have to be sorted after year.

What I do is get an array of all the items and sort the type. Then I take the sortedArray and push each item with the same type in a array (Which is a 2D array), so I have them as seperate array. Then I wanna sort the year of each array of the 2D array

This is my code so far

function sortDivs(type, reverse) {

    let sortedList = items.map((e) => ({ // Map each item to a object with its element and its child element's text
        e, // element
        text: e.querySelector('.wrap.toggler .'+type).textContent, // the text to be compared
        year: e.querySelector('.wrap.toggler .'+'jahr').textContent.replace(' ', '').replace('-', '') // The year to compare

    })).sort(({text:a},{text:b}) => 

        reverse ? b.localeCompare(a) : a.localeCompare(b)

    )

    // if type 'year' no need to sort second layer
    if(type == 'jahr') return

    // Sort each duplicate type of clicked tab per year , newest top

    let currentText = sortedList[0].text

    let tmpArray = []
    let array2D = []

    // Push same items in seperate arrays of array2D
    sortedList.forEach((item, i) => { 

        if(item.text != currentText) {

            array2D.push(tmpArray)

            tmpArray = []

            currentText = item.text
        }

        if(item.text == currentText) {

            tmpArray.push(item)
        }

        if(i == sortedList.length - 1) {

            array2D.push(tmpArray)
        }
    })


    console.log('Seperated Arrays ', array2D)

    // Everything works till here... The arrays dont get sorted
    // Sort each array of array2D
    tmpArray = []

    array2D.forEach(array => {

        array.sort(({year:a}, {year:b}) =>  {

            b.localeCompare(a)

        })

    })

    console.log('seperate sorted arrays ',array2D)

    array2D.forEach(e =>  {

            console.log('final', e)

    })
}

It works that I get all the sorted types and store them in seperate array. But when looping through the 2D array and sorting each array doesnt work. I get unsorted arrays.. 2019 needs to be first.

enter image description here

The array structure is seen on the screenshot or stringified here:

`
[
[
{"e":{"jQuery111307164768320580499":8},"text":"Fertiggestellt ","year":"2018"},
{"e":{"jQuery111307164768320580499":9},"text":"Fertiggestellt ","year":"2017"},
{"e":{"jQuery111307164768320580499":21},"text":"Fertiggestellt ","year":"2013"},
{"e":{"jQuery111307164768320580499":25},"text":"Fertiggestellt ","year":"2019"},
{"e":{"jQuery111307164768320580499":27},"text":"Fertiggestellt ","year":"2017"},
{"e":{"jQuery111307164768320580499":28},"text":"Fertiggestellt ","year":"2017"},
{"e":{"jQuery111307164768320580499":29},"text":"Fertiggestellt ","year":"2017"},
{"e":{"jQuery111307164768320580499":30},"text":"Fertiggestellt ","year":"2014"},
{"e":{"jQuery111307164768320580499":31},"text":"Fertiggestellt ","year":"2014"},
{"e":{"jQuery111307164768320580499":32},"text":"Fertiggestellt ","year":"2013"},
{"e":{"jQuery111307164768320580499":33},"text":"Fertiggestellt ","year":"2013"},
{"e":{"jQuery111307164768320580499":34},"text":"Fertiggestellt ","year":"2013"},
{"e":{"jQuery111307164768320580499":35},"text":"Fertiggestellt ","year":"2012"},
{"e":{"jQuery111307164768320580499":36},"text":"Fertiggestellt ","year":"2012"},
{"e":{"jQuery111307164768320580499":37},"text":"Fertiggestellt ","year":"2009"},
{"e":{"jQuery111307164768320580499":38},"text":"Fertiggestellt ","year":"2006"},{"e":{"jQuery111307164768320580499":39},"text":"Fertiggestellt ","year":"2005"},
{"e":{"jQuery111307164768320580499":40},"text":"Fertiggestellt ","year":"2005"},
{"e":{"jQuery111307164768320580499":41},"text":"Fertiggestellt ","year":"2001"},
{"e":{"jQuery111307164768320580499":42},"text":"Fertiggestellt ","year":"2009"}
]
,
[
{"e":{"jQuery111307164768320580499":6},"text":"im Bau ","year":"2017"},{"e":{"jQuery111307164768320580499":7},"text":"im Bau ","year":"2017"},{"e":{"jQuery111307164768320580499":26},"text":"im Bau ","year":"2011"}
],
[
{"e":{"jQuery111307164768320580499":4},"text":"in Planung ","year":"2018"},
{"e":{"jQuery111307164768320580499":5},"text":"in Planung ","year":"2018"},
{"e":{"jQuery111307164768320580499":10},"text":"in Planung ","year":"2017"}
],
[
{"e":{"jQuery111307164768320580499":20},"text":"Machbarkeitsstudie ","year":"2016"}
],
[
{"e":{"jQuery111307164768320580499":11},"text":"Wettbewerb ","year":"2019"},
{"e":{"jQuery111307164768320580499":12},"text":"Wettbewerb ","year":"2018"},
{"e":{"jQuery111307164768320580499":13},"text":"Wettbewerb ","year":"2018"},
{"e":{"jQuery111307164768320580499":14},"text":"Wettbewerb ","year":"2016"},
{"e":{"jQuery111307164768320580499":15},"text":"Wettbewerb ","year":"2017"},
{"e":{"jQuery111307164768320580499":16},"text":"Wettbewerb ","year":"2015"},
{"e":{"jQuery111307164768320580499":17},"text":"Wettbewerb ","year":"2018"},
{"e":{"jQuery111307164768320580499":18},"text":"Wettbewerb ","year":"2017"},
{"e":{"jQuery111307164768320580499":19},"text":"Wettbewerb ","year":"2016"},
{"e":{"jQuery111307164768320580499":22},"text":"Wettbewerb ","year":"2015"},
{"e":{"jQuery111307164768320580499":23},"text":"Wettbewerb ","year":"2015"},
{"e":{"jQuery111307164768320580499":24},"text":"Wettbewerb ","year":"2015"}
]
]
`
2 Answers

You are missing a return in your second sort

array2D.forEach(array => {

    array.sort(({year:a}, {year:b}) =>  {

        return b.localeCompare(a)

    })

})

You can also write this as an expression without a return

array.sort(({year:a}, {year:b}) => b.localeCompare(a))

Working sandbox here https://codesandbox.io/s/sorting2ds-735qm

Try to change the sort function like that

    array2D = array2D.map(array => {
        return array.sort((a, b) => {
            if (Number(a.year) > Number(b.year)) {
                return -1;
            }
            return 1;
        });
    })
Related