sorting

JavaScript format array of objects into nested children

筅森魡賤 提交于 2021-02-04 18:52:08
问题 I have an array of objects with parentId and sort values that I'd like to put into an array with nested 'children' and sorted appropriately. For example, here's the data: [{ id: 1, sort: 2, parentId: null, name: 'A' }, { id: 2, sort: 1, parentId: 1, name: 'A.1' }, { id: 3 sort: 2, parentId: 1, name: 'A.2' }, { id: 4, sort: 1, parentId: null, name: 'B' }] The way I'd like to transform this would be such as: [{ id: 4, sort: 1, parentId: null, name: 'B', children: [] }, { id: 1, sort: 2,

Javascript - sorting a collection of divs

早过忘川 提交于 2021-02-04 18:08:17
问题 I am trying to learn Javascript alone so please don't suggest a library or jQuery. I have a list of divs and I want the user to be able to sort them by their value. For example: <button onclick="sort();">Test</button> <div class="num">2</div> <div class="num">3</div> <div class="num">8</div> <div class="num">1</div> JS: function sort(){ var elements = document.getElementsByClassName("num"); elements.sort(); } I cannot find a straight answer to what's wrong with this. Does

Javascript - sorting a collection of divs

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-04 18:07:30
问题 I am trying to learn Javascript alone so please don't suggest a library or jQuery. I have a list of divs and I want the user to be able to sort them by their value. For example: <button onclick="sort();">Test</button> <div class="num">2</div> <div class="num">3</div> <div class="num">8</div> <div class="num">1</div> JS: function sort(){ var elements = document.getElementsByClassName("num"); elements.sort(); } I cannot find a straight answer to what's wrong with this. Does

Javascript - sorting a collection of divs

泄露秘密 提交于 2021-02-04 18:07:28
问题 I am trying to learn Javascript alone so please don't suggest a library or jQuery. I have a list of divs and I want the user to be able to sort them by their value. For example: <button onclick="sort();">Test</button> <div class="num">2</div> <div class="num">3</div> <div class="num">8</div> <div class="num">1</div> JS: function sort(){ var elements = document.getElementsByClassName("num"); elements.sort(); } I cannot find a straight answer to what's wrong with this. Does

Javascript - sorting a collection of divs

跟風遠走 提交于 2021-02-04 18:06:58
问题 I am trying to learn Javascript alone so please don't suggest a library or jQuery. I have a list of divs and I want the user to be able to sort them by their value. For example: <button onclick="sort();">Test</button> <div class="num">2</div> <div class="num">3</div> <div class="num">8</div> <div class="num">1</div> JS: function sort(){ var elements = document.getElementsByClassName("num"); elements.sort(); } I cannot find a straight answer to what's wrong with this. Does

Custom comparer datagridview sort

一个人想着一个人 提交于 2021-02-04 16:35:27
问题 I have a datagridview with a bindingsource as datasource, and the bindingsource has a datatable as a datasource. Some columns are strings but I want them to be sorted in a specific way. The grid sorts them as 1, 10, 10,0 44a, 6c. But I want them to sorted: 1, 6c, 10, 44a, 100 as if i would take the numbers only from the values and sort them accordingly. Is there a way I cand add a custom comparer when certain columns are being sorted? Any other soulutions would be ok if the grid,

Javascript sort array of objects using array of priority

巧了我就是萌 提交于 2021-02-04 16:24:29
问题 I have this array of objects: var eventList = [ { eventName: "abc", status: "completed" }, { eventName: "def", status: "live" }, { eventName: "ghi", status: "live" }, { eventName: "jkl", status: "upcoming" }, ] I want to sort these array of objects using a priority array of a specific key, say ["live", "upcoming", "completed"] for status, meaning all live events come first, followed by upcoming followed by completed. Answers all over the internet seem like you can only sort array objects

Sort strings by the first N characters

喜夏-厌秋 提交于 2021-02-04 15:08:35
问题 I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python? 回答1: sorted(array, key=lambda x:x[:24]) Example: >>> a = ["wxyz", "abce", "abcd", "bcde"] >>> sorted(a) ['abcd', 'abce', 'bcde', 'wxyz'] >>> sorted(a, key=lambda

Sort strings by the first N characters

大兔子大兔子 提交于 2021-02-04 15:08:20
问题 I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python? 回答1: sorted(array, key=lambda x:x[:24]) Example: >>> a = ["wxyz", "abce", "abcd", "bcde"] >>> sorted(a) ['abcd', 'abce', 'bcde', 'wxyz'] >>> sorted(a, key=lambda

Sort strings by the first N characters

半城伤御伤魂 提交于 2021-02-04 15:08:12
问题 I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python? 回答1: sorted(array, key=lambda x:x[:24]) Example: >>> a = ["wxyz", "abce", "abcd", "bcde"] >>> sorted(a) ['abcd', 'abce', 'bcde', 'wxyz'] >>> sorted(a, key=lambda