Home > Software engineering >  Java Data Manipulation
Java Data Manipulation

Time:03-20

I've come across some data and I wanted to sort it many different ways for example sorting by the cheapest product with the most buys. I'm wanting to group the document row by row as each row contains another 'item'. I've attached an image for reference. I prefer to use Java but will learn R if I have to. Do I manually code each line into an array? There's 400 items and I can break it up into days if that's the only way.

Sample

CodePudding user response:

You can store the data in an array of arrays and then use custom comparators to sort it. An example of this would be:

public static void main(String[] args){
    ArrayList<String[]> data = new ArrayList<>();
    String[] firstFruit = new String[]{"Banana", "Yellow", "7 pound"};
    String[] secondFruit = new String[]{"Cola Apple", "Red", "1 pound"};

    data.add(firstFruit);
    data.add(secondFruit);
    System.out.println("Unsorted Array");
    print(data);

    Comparator<String[]> compareWeights= Comparator.comparingInt((String[] item) -> Integer.parseInt(item[2].split(" ")[0]));

    data.sort(compareWeights);
    System.out.println("Sorted Array");
    print(data);
}

static void print(ArrayList<String[]> data){
    System.out.println("-----------------");
    for (String[] str:data) {
        System.out.println(Arrays.toString(str));
    }
    System.out.println("-----------------");
}

Here, you are sorting according to weight, if you want to sort some other way then you will have to create another comparator.

The output of this code is:

Unsorted Array
-----------------
[Banana, Yellow, 7 pound]
[Cola Apple, Red, 1 pound]
-----------------
Sorted Array
-----------------
[Cola Apple, Red, 1 pound]
[Banana, Yellow, 7 pound]
-----------------

CodePudding user response:

Another way to do this would be to create a class called fruit and create comparators in that class. For example:

import java.awt.*;
import java.util.Comparator;
public class Fruit{

    public static Comparator<Fruit> compareWeights = Comparator.comparingInt((Fruit fruit) -> fruit.weight);
    public static Comparator<Fruit> compareColors = Comparator.comparingInt((Fruit fruit) -> fruit.color.getRGB());

    private final String fruitName;
    private final int weight;
    private final Color color;

    Fruit(String fruitName, Color color, int weight){
        this.fruitName = fruitName;
        this.weight = weight;
        this.color = color;

    }

    @Override
    public String toString(){
        return fruitName   ", "   color.toString()   ", "   weight   " pound.";
    }
}

And then you create fruits and sort them with the custom comparators

    Fruit firstFruit = new Fruit("Banana", Color.YELLOW, 1);
    Fruit secondFruit = new Fruit("Cola apple", Color.RED, 7);

    ArrayList<Fruit> data = new ArrayList<>();
    data.add(secondFruit);
    data.add(firstFruit);

    data.sort(Fruit.compareColors);

    System.out.println(data);
  • Related