Reputation: 9
So I am trying to learn how to sort arrays for a class project. I was wondering how I can sort one array and consequently sort another. In the code below I am able to sort the years array, but how would I make it so that changing this one array would change both the names and artists arrays to that they line up? Also, if you have any tips on making the code a lot less harsh on the eyes, please let me know, I am struggling to grasp this concept.
public class Main
{
public static int q;
public static int t;
public static int u;
public static int v;
public static int w = -1;
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static String[] names;
public static int[] years;
public static String[] artists;
public static String temp[];
public static int tempO[];
public static String[] PostMalone;
public static String[] MichaelJackson;
public static String[] ElvisPresley;
public static class Music {
private int year;
private String title;
private String artist;
private int placement;
// Constructor for objects of class Music
Music(String t, int y, String a)
{
// initialize instance variables
this.title = t;
this.year = y;
this.artist = a;
setPlacement();
}
public void setPlacement() {
w+=1;
this.placement = w;
}
public int getPlacement() {
return placement;
}
public String getTitle()
{
return title;
}
public void setTitle(String t)
{
title = t;
}
public String getArtist()
{
return artist;
}
public void setArtist(String a)
{
artist = a;
}
public int getYear()
{
return year;
}
public void setTitle(int y)
{
year = y;
}
}
public static void arrNames(Music name) {
temp = new String[x];
int g = x;
int h = 0;
while (g>0) {
temp[h] = names[h];
g-=1;
h+=1;
}
x+=1;
names = new String[x];
g = x;
h = 0;
while (g>1) {
names[h] = temp[h];
g-=1;
h+=1;
}
names[(x-1)] = name.getTitle();
}
public static void arrYear(Music name) {
tempO = new int[y];
int g = y;
int h = 0;
while (g>0) {
tempO[h] = years[h];
g-=1;
h+=1;
}
y+=1;
years = new int[y];
g = y;
h = 0;
while (g>1) {
years[h] = tempO[h];
g-=1;
h+=1;
}
years[(y-1)] = name.getYear();
}
public static void arrArtist(Music name) {
temp = new String[z];
int g = z;
int h = 0;
while (g>0) {
temp[h] = artists[h];
g-=1;
h+=1;
}
z+=1;
artists = new String[z];
g = z;
h = 0;
while (g>1) {
artists[h] = temp[h];
g-=1;
h+=1;
}
artists[z-1] = name.getArtist();
}
public static void setGroup(Music name) {
arrNames(name);
arrYear(name);
arrArtist(name);
}
public static void getGroups() {
int a = x;
int b = y;
int c = z;
int d = 0;
int e = 0;
int f = 0;
System.out.println("Names Category: ");
while (a > 0) {
System.out.print(names[d] + " ");
d+=1;
a-=1;
}
System.out.println("");
System.out.println("Years Category: ");
while (b > 0) {
System.out.print(years[e] + " ");
e+=1;
b-=1;
}
System.out.println("");
System.out.println("Artists Category: ");
while (c > 0) {
System.out.print(artists[f] + " ");
f+=1;
c-=1;
}
System.out.println("");
}
public static void sortByYear(int arr[], int n)
{
int i, l, j;
for (i = 1; i < n; i++)
{
l = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > l)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = l;
}
}
public static void printArray(int[] array)
{
System.out.print("Here are all of the release dates in order: ");
for (int l = 0; l < array.length; l++) {
System.out.print(array[l] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Music CH = new Music("Can't Help Falling In Love", 1961, "Elvis Presley");
setGroup(CH);
Music SF = new Music("Sunflower", 2018, "Post Malone");
setGroup(SF);
Music JR = new Music("Jailhouse Rock", 1957, "Elvis Presley");
setGroup(JR);
Music BI = new Music("Beat It", 1982, "Michael Jackson");
setGroup(BI);
Music TH = new Music("Thriller", 1982, "Michael Jackson");
setGroup(TH);
Music CG = new Music("Congratulations", 2016, "Post Malone");
setGroup(CG);
Music BL = new Music("Burning Love", 1972, "Elvis Presley");
setGroup(BL);
Music SC = new Music("Smooth Criminal", 2001, "Michael Jackson");
setGroup(SC);
Music BN = new Music("Better Now", 2018, "Post Malone");
setGroup(BN);
Music RS = new Music("Rockstar", 2018, "Post Malone");
setGroup(RS);
getGroups();
sortByYear(years, x);
printArray(years);
getGroups(); //error here, I want the names category and artists category to line up with the years category. How would I do that?
}
}
Upvotes: 0
Views: 107
Reputation: 88757
I'll expand on my comment a little:
Assuming you have the following arrays:
int[] years = ...;
String[] titles = ...;
String[] artists = ...;
Instead of using those try to create a Song
array, e.g. like this:
//simplified class, use proper access modifiers, getters and setters
class Song {
int year;
String title;
String artist;
}
Song[] songs = ...;
Then sort it using a Comparator<Song>
, e.g. like this:
Arrays.sort(songs, new Comparator<Song>() {
public int compare(Song left, Song right) {
return Integer.compare(left.year, right.year);
}
});
This deliberately didn't use lambdas to make it easier to understand for a beginner. Here's how it could look like with lambdas:
Arrays.sort(songs, Comparator.comparing(song -> song.year));
Upvotes: 4