简体   繁体   English

如何在 java 中对一个数组进行排序,然后对另一个数组进行排序?

[英]How do I sort one array and consequently sort another in java?

So I am trying to learn how to sort arrays for a class project.所以我正在尝试学习如何为 class 项目对 arrays 进行排序。 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?在下面的代码中,我可以对 years 数组进行排序,但是我将如何做到这一点,以便更改这个数组会将名称和艺术家 arrays 更改为他们排列的名称? 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?
    }
}

I'll expand on my comment a little:我将稍微扩展一下我的评论:

Assuming you have the following arrays:假设您有以下 arrays:

int[] years = ...;
String[] titles = ...;
String[] artists = ...;

Instead of using those try to create a Song array, eg like this:而不是使用那些尝试创建一个Song数组,例如:

//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> , eg like this:然后使用Comparator<Song>对其进行排序,例如:

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.这故意不使用 lambdas 以便初学者更容易理解。 Here's how it could look like with lambdas:这是使用 lambda 的样子:

 Arrays.sort(songs, Comparator.comparing(song -> song.year));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM