C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Lexicographical sorting of matrix

By : JoelS
Date : January 12 2021, 07:00 PM
will help you I'm required to sort the matrix(each row is a feature vector) in lexicographical order. , According to Sardar Usama's comment I'm using the test matrix:
code :
``````A = [10 40 30 -1;
43 78 -5 1;
87 54 -4 -2];
``````
``````% ascending sorting of every row vector
sort(A,2)
ans =
-1    10    30    40
-5     1    43    78
-4    -2    54    87

% ascending sorting of every column vector
sort(A,1)
ans =
10    40    -5    -2
43    54    -4    -1
87    78    30     1
``````
``````sortrows(A,1)
ans = 10    40    30    -1
43    78    -5     1
87    54    -4    -2
``````

Share :

## Lexicographical sorting

By : user2608655
Date : March 29 2020, 07:55 AM
it helps some times It seems that what you're looking for is a better understanding of the question, so let me just make it clear. The usual sorting on strings is lexicographic sorting. If you sort the strings [jibw, ji, jp, bw, jibw] into lexicographic order, the sorted sequence is [bw, ji, jibw, jibw, jp], which is what you got. So your problem is not with understanding the word "lexicographic"; you already understand it correctly.
Your problem is that you're misreading the question. The question doesn't ask you to sort the strings in lexicographic order. (If it did, the answer you got by sorting would be correct.) Instead, it asks you to produce one string, got by concatenating the input strings in some order (i.e., making one string without spaces), so that the resulting single string is lexicographically minimal.
code :
``````bwjijibwjibwjp //Your answer
``````

## Sorting in lexicographical order

By : vmwong
Date : March 29 2020, 07:55 AM
I hope this helps you . First the spaceship operator is <=> not <==>
Secondly you're not combining the 2 comparisons correctly: the result of the comparison will be -1,0,or 1. These are all truthy values and true && foo is just foo, so your code would just sort by the y values
code :
``````x_ordering = a.x <=> b.x
x_ordering == 0 ? a.y <=> b.y : x_ordering
``````
``````array.sort! { |a,b| [a.x, a.y] <=> [b.x, b.y]}
``````
`````` array.sort_by! { |a| [a.x, a.y] }
``````

## Sorting dictionary by value and lexicographical

By : Oscar
Date : March 29 2020, 07:55 AM
To fix the issue you can do To match the actual output you want you have to use two keys to sort negating the int value with -:
code :
``````d = {'a':10,'b':20,'c':5,'d':5,'e':5}

for k,v in sorted(d.items(),key=lambda x:(-x[1],x[0])):
print("{} {}".format(k,v))
``````
``````b 20
a 10
c 5
d 5
e 5
``````

## Sorting a String in lexicographical order

By : user1521924
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The question provides an input String and an integer, it asks us to convert the string into all the possible combinations of sub-strings of length as specified by the input integer provided. Then we have to find the maximum and minimum from those sub-strings. , These two substring will be the start
code :
``````public static void main(String... args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int k = sc.nextInt();
String maxString = s.substring(0, k);
String minString = s.substring(0, k);

for (int i = 1; i <= s.length() - k; ++i){
String curSubstr = s.substring(i, i + k);
System.out.println(curSubstr);
if (maxString.compareTo(curSubstr) < 0)
maxString = s.substring(i, i + k);
if (minString.compareTo(curSubstr) > 0)
minString = s.substring(i, i + k);
}

System.out.println(minString);
System.out.println(maxString);
}
``````
``````Case #1
Input:
HelloWorld
2
Output:
el
ll
lo
oW
Wo
or
rl
ld
He // the min
rl // the max
``````

## Sorting in Map (Lexicographical Order)

By : user2905886
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If you want the entries of a map sorted in a particular order (and the default order is operator <, which doesn't do what you're asking), then you need to instantiate your map with a custom comparator.
code :
``````struct myComp {
bool operator()(const std::pair<std::string, int>& lhs,
const std::pair<std::string, int>& rhs) const
{ /* your code here */ }
};

std::map<std::pair<std::string, int>, int, myComp> m;
``````