sorting

React/react hooks: child component is not re-rendering after the state is changed?

Deadly 提交于 2020-08-20 12:15:17
问题 I am writing a code in react/react hooks that attempt to do the following. Get an array of objects from the parent component as a prop Set it as a state using useState hook. Sort the state according to the intended filters (Time and Rating), and re-render the child component. What I see is that the below code updates the state after sorting, but even though the state is updated, the child component that is dependent on the state would not re-render. I thought the child component would

Sort the fields in a struct based on value

萝らか妹 提交于 2020-08-20 06:16:18
问题 Say I have a struct: MyStruct.a = 12; MyStruct.b = 22; MyStruct.c = 32; Can I modify it so that the fields are ordered based on their value: MyStruct c: 32 b: 22 a: 12 The orderfields methods allow ordering of the struct based on the field name or other structures/cell arrays, but not by value. 回答1: % Define initial structure: myStruct.a = 12; myStruct.b = 22; myStruct.c = 32; % Find desired order of values, rather than fieldnames: [ ~,sortIdx ] = sort( structfun( @(x) x, myStruct ), 'descend

Sort the fields in a struct based on value

狂风中的少年 提交于 2020-08-20 06:16:07
问题 Say I have a struct: MyStruct.a = 12; MyStruct.b = 22; MyStruct.c = 32; Can I modify it so that the fields are ordered based on their value: MyStruct c: 32 b: 22 a: 12 The orderfields methods allow ordering of the struct based on the field name or other structures/cell arrays, but not by value. 回答1: % Define initial structure: myStruct.a = 12; myStruct.b = 22; myStruct.c = 32; % Find desired order of values, rather than fieldnames: [ ~,sortIdx ] = sort( structfun( @(x) x, myStruct ), 'descend

From a set of pairs, find all subsets s.t. no pair in the subset shares any element with a pair not in the subset

有些话、适合烂在心里 提交于 2020-08-20 06:12:57
问题 I have a set of pairs. Each pair is represented as [i,1:2] . That is, the ith pair are the numbers in the first and second column in the ith row. I need to sort these pairs into distinct groups, s.t. there is no element in any pair in the jth group that is in any group not j . For example: EXAMPLE 1: DATA > col1 <- c(3, 4, 6, 7, 10, 8) > col2 <- c(6, 7, 3, 4, 3, 1) > > dat <- cbind(col1, col2) > rownames(dat) <- 1:nrow(dat) > > dat col1 col2 1 3 6 2 4 7 3 6 3 4 7 4 5 10 3 6 8 1 For all pairs,

Lexicographically sorted list of lists of strings

夙愿已清 提交于 2020-08-19 14:07:48
问题 Currently, I am trying to implement a code to generate frequent sequences. In doing so I need to get an in-place sort of a list of lists of strings as follows: List<List<string>> myList = new List<List<string>>(); List<string> input1 = new List<string>() {"a", "b", "d"}; List<string> input2 = new List<string>() {"a", "b", "c"}; myList.Add(input1); myList.Add(input2); The output that I need is: myList = {{"a","b","c"},{"a","b","d"}}; I have tried to use myList.Sort() but it raised a System

Lexicographically sorted list of lists of strings

ε祈祈猫儿з 提交于 2020-08-19 14:03:30
问题 Currently, I am trying to implement a code to generate frequent sequences. In doing so I need to get an in-place sort of a list of lists of strings as follows: List<List<string>> myList = new List<List<string>>(); List<string> input1 = new List<string>() {"a", "b", "d"}; List<string> input2 = new List<string>() {"a", "b", "c"}; myList.Add(input1); myList.Add(input2); The output that I need is: myList = {{"a","b","c"},{"a","b","d"}}; I have tried to use myList.Sort() but it raised a System

How to sort based on incomplete criteria?

徘徊边缘 提交于 2020-08-19 10:23:33
问题 First I tried passing my own function to Array.sort , but it doesn't sort correctly. Notice how 'c' comes before 'a' in the result, even though the case if (b == 'a' && a == 'c') is handled correctly. These data are just for example. My actual data is not to be alphabetically sorted. It must use the logic illustrated in the a_before_b and b_before_a functions. Since I only have conditions to determine the relative ordering of SOME (NOT all) pairs of elements, there may be multiple valid

How to sort based on incomplete criteria?

瘦欲@ 提交于 2020-08-19 10:21:58
问题 First I tried passing my own function to Array.sort , but it doesn't sort correctly. Notice how 'c' comes before 'a' in the result, even though the case if (b == 'a' && a == 'c') is handled correctly. These data are just for example. My actual data is not to be alphabetically sorted. It must use the logic illustrated in the a_before_b and b_before_a functions. Since I only have conditions to determine the relative ordering of SOME (NOT all) pairs of elements, there may be multiple valid

How to sort based on incomplete criteria?

僤鯓⒐⒋嵵緔 提交于 2020-08-19 10:21:37
问题 First I tried passing my own function to Array.sort , but it doesn't sort correctly. Notice how 'c' comes before 'a' in the result, even though the case if (b == 'a' && a == 'c') is handled correctly. These data are just for example. My actual data is not to be alphabetically sorted. It must use the logic illustrated in the a_before_b and b_before_a functions. Since I only have conditions to determine the relative ordering of SOME (NOT all) pairs of elements, there may be multiple valid

Sorting array of string in a specific format A-Z AA-ZZ

扶醉桌前 提交于 2020-08-11 23:59:31
问题 I have a array in javascript like this [A,AA,CC,DD,B,C] I want it to be sorted like this [A,B,C,AA,CC,DD] 回答1: You can first sort by string length and then alphabetically var arr = ['A', 'B', 'C', 'AA', 'CC', 'DD']; var result = arr.sort(function(a, b) { return a.length - b.length || a.localeCompare(b) }) console.log(result) 回答2: You could sort first by length of the strings and then by value. var array = ['A', 'AA', 'B', 'C', 'CC', 'DD']; array.sort(function (a, b) { return a.length - b