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?
}
}
year, name, artistetc., i.e. all the elements that you'd distribute over those arrays but want to have sorted in the same order. Then use aComparatorto sort that array of objects.Comparator), 3) iterate through the sorted array and rebuild the others (make copies) by using the current iteration index as the new index and the "original index" in the "year" array elements to get the corresponding values from the original "names" array etc.