0

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?
    }
}

3
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Mar 28, 2022 at 15:05
  • 1
    Instead of using multiple arrays you should try to create classes that contain year, name, artist etc., i.e. all the elements that you'd distribute over those arrays but want to have sorted in the same order. Then use a Comparator to sort that array of objects. Commented Mar 28, 2022 at 15:11
  • If you need to keep separate arrays for some reason then try the following: 1) create an object to contain "year" and "original index" (which is the index in the unsorted original array), 2) sort that years array (again use a 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. Commented Mar 28, 2022 at 15:13

1 Answer 1

4

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));
Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't that just be what Music(String t, int y, String a) is? And how would I set that to a single array?
@Pendragonz yes, it seems like Music would be what you could use. I didn't use that class because the name is somewhat misleading and your code is lacking so I didn't know whether it would be usable or not. You'd basically use the same approach I described, just replace Song by Music and use getYear() instead of directly accessing the year property.
Is there a way I can send you my code? I retyped it up but Im getting some sort of error I don't understand in the comparator section, mainly because I've never used comparator before.
There are two errors, one for Comparator and one for Arrays that both say "cannot find symbol"
@Pendragonz you probably didn't import those classes. If you're using an IDE like IntelliJ Idea or Eclipse it should suggest a fix (i.e. add the imports), if you're not using an IDE then you need to do it yourself or start using an IDE. Also try to understand the errors and do some research, we can write the code for you - it's your task so you need to solve it. Also keep in mind that we're investing our time into this so use it sparingly and wisely. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.