Get the App
SLTechnology News&Howtos  ›  Development  › 

How to sort two arrays with Java

Shulou Source: shulou.com Published: 2022-06-02 18:22:51 09月18日 Update

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!

Tags: Sort two arrays numbers learning elements content methods questions codes priorities functions that is ideas situations articles more knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft OPPO Reno Linux Shulou Technology