Sorting Arrays in Java

I want to pass 2 arrays of functions to Java and sort them in the calling function. How to use the function for this?

I could return the function to an object with two arrays, but is there an object-oriented solution for this?

EDIT: In this particular situation, I cannot use the built-in Array.sort function in Java. Let's say that 2 arrays are height and weight. They have the same length, and the same index corresponds to the height and weight of the same person in both arrays. I want to sort an array of heights in ascending order, sorting an array of weights corresponding to an array of heights. Therefore, using the sort function will ruin the relationship between the two arrays.

+3
source share
5 answers

When you pass an array to a function, it is not copied. Just a link to it is copied and passed to a function that points to the same place. You just need to sort the arrays in place.

EDIT: to solve the actual sorting problem, you can use any sorting algorithm to sort the array height. The only difference is that when you change two elements in the sorting process height, you must also swap the corresponding elements in the array weight.

+4
source
public void sort2(Object o1[], Object o2[])
{
  Arrays.sort(o1);
  Arrays.sort(o2);
}

A bit trickier:

public <T> void sort2(T o1[], T o2[],  Comparator<? super T> c)
{
  Arrays.sort(o1, c);
  Arrays.sort(o2, c);
}

EDIT: , , . , Comparable Person . , , , .

+5

, . , , .

- / , . 1 Person, , . , compareTo(), 1.

2 junit , Person s. , . , , .

1 - Person



public class Person implements Comparable {
    private Float height;
    private Float weight;
    private String name;

    public Person(){}

    public Person(Float height, Float weight, String name) {
        this.height = height;
        this.weight = weight;
        this.name = name;
    }

    public Float getHeight() {
        return height;
    }
    public void setHeight(Float height) {
        this.height = height;
    }
    public Float getWeight() {
        return weight;
    }
    public void setWeight(Float weight) {
        this.weight = weight;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int compareTo(Person other) {
        //sort by height ascending
        return this.height.compareTo(other.getHeight());
    }
}

2 - junit



import junit.framework.TestCase;
import java.util.*;

public class PersonTest extends TestCase {

    private List personList = new ArrayList();

    public PersonTest(String name) {
        super(name);
    }

    public void testCompareTo() {
        personList.add(new Person(72F,125F,"Bob"));// expect 3rd when sorted by height asc
        personList.add(new Person(69.9F,195F,"Jack"));// expect 2nd when sorted by height asc
        personList.add(new Person(80.05F,225.2F,"Joe"));// expect 4th when sorted by height asc
        personList.add(new Person(57.02F,89.9F,"Sally"));// expect 1st when sorted by height asc
        Collections.sort(personList);
        assertEquals("Sally should be first (sorted by height asc)",personList.get(0).getName(),"Sally");
        assertEquals("Jack should be second (sorted by height asc)",personList.get(1).getName(),"Jack");
        assertEquals("Bob should be third (sorted by height asc)",personList.get(2).getName(),"Bob");
        assertEquals("Joe should be fourth (sorted by height asc)",personList.get(3).getName(),"Joe");

        Collections.sort(personList,new Comparator() {
            public int compare(Person p1, Person p2) {
                //sort by weight ascending
                return p1.getWeight().compareTo(p2.getWeight());
            }
        });
        assertEquals("Sally should be first (sorted by weight asc)",personList.get(0).getName(),"Sally");
        assertEquals("Bob should be second (sorted by weight asc)",personList.get(1).getName(),"Bob");
        assertEquals("Jack should be third (sorted by weight asc)",personList.get(2).getName(),"Jack");
        assertEquals("Joe should be fourth (sorted by weight asc)",personList.get(3).getName(),"Joe");      
    }

}

+1

, , A B. B A ?

​​Java, B, A .

# out, , out.

0

You can return an array of arrays or an object that contains two arrays. However, it looks like the values ​​in the two arrays should be matched, so you really should have one array of objects that contains two values.

BTW: I would never use Float and swim, I would avoid (as it was only in 6 places), I would suggest using int, long or double.

0
source

Source: https://habr.com/ru/post/1710922/


All Articles