How to sort two arrays with Java
This article mainly explains "how to sort two arrays with Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to sort two arrays with Java".
Suppose you have number,group, two arrays whose elements are numbers, and now the rules to sort them are as follows:
1. If the element in group exists in numbers,
two。 To put the numbers that appear in group before those in number, pay attention to sorting
Numbers = [8, 3, 1, 2, 5, 4, 4, 7] group = {2, 3, 3, 5, 5, 7}
After analysis, it should be a matter of priority, and the method of tuple sorting is used here:
Numbers = [8, 3, 1, 2, 5 Magazine 4 7 Magazine 6] group = {2 Magne3 Magazine 5 7} tmp = [] for x in numbers: if x in group: tmp.append ((0, x)) else: tmp.append ((1, x)) tmp.sort () print (list (map (lambda XRX [1], tmp)
I saw a classic code on the Internet:
Def sort_priority (values,group): def helper (x): if x in group: return (0, x) return (1, x) values.sort (key=helper) numbers = [8,3,1,2,5 sort_priority (numbers, group) print (numbers)
Recommend the method of using the second higher order function
Thank you for your reading, the above is the content of "how to sort two arrays with Java". After the study of this article, I believe you have a deeper understanding of how to sort two arrays with Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!