Java Object array Multi-attribute conditional ordering problem (detailed) __java

Source: Internet
Author: User
Tags gety static class
Recently encountered a multiple conditional sorting problem, similar to the " something has a,b,c three properties, compare the a,a when the same condition is the same when the b,b condition is the same when comparing C, sorted output"In the form. This kind of topic uses the C + + solution to be very handy, may use the structure body, unifies the sort and the compare, can complete the thought. But how do we solve the problem with Java? Java is Object-orientedThe language, without the concept of structural body, we should define the class. For example, use a team ranking problem to give examples. Now the object is team info info, with four attributes: Team name, Team X, net win Y, goal number Z. So we define classes like this:
Class Info {
		private String name;
		private int x;
		private int y;
		private int z;

		Public String GetName () {return
			name;
		}

		public void SetName (String name) {
			this.name = name;
		}

		public int Getx () {return
			x;
		}

		public void setx (int x) {
			this.x = x;
		}

		public int gety () {return
			y;
		}

		public void sety (int y) {
			this.y = y;
		}

		public int Getz () {return
			z;
		}

		public void Setz (int z) {
			this.z = z;
		}
	}

Each property has a get and set two method corresponding respectively, to explain briefly, for example, the SetName () method of the name attribute, which is to set the name, we need to call it when assigning the object's name attribute in the main function, and tell the program by the specific parameter value what name you want to set it. , which is not required to return a value, that is, the reason that the definition type is void; then the GetName () method is to get the name, it does not need arguments, the program returns the name through the return value, so its return value type is string. The remaining few extrapolate can be the same principle.
So the question below is, how do you write the main function? We consider one of the most complex situations, is to enter the number of data group N, and then enter each group of data, each group of properties contains four attributes of the object, this is to use an array of objects to implement, the introduction of the object array A [], a[I] in the first group of data objects in the four properties. Next, add an array of objects to set B, which makes it easier for us to sort the conditions of multiple attributes.

                Scanner sc = new Scanner (system.in);
		int N = Sc.nextint ();
		String temp = Sc.nextline (); Input N and press ENTER will be read as nextline, if there is no temp to filter out this enter, there will be a certain impact on the following
		info[] a = new info[n+1];
		List<info> B = new ArrayList ();
		
		for (int i = 0; i < N; i++) {
			String str = sc.nextline ();
			String[] s = Str.split ("");
			A[i] = new Info (); This line is very important
			a[i].setname (s[0]);
			A[i].setx (Integer.parseint (s[1));
			A[i].sety (Integer.parseint (s[2));
			A[i].setz (Integer.parseint (s[3));
			B.add (A[i]);
		


The rest of the problem is how to achieve the ordering of multiple attributes, assuming that we do this: first, compare the integral x, if the integral x is the same as the net wins y, if the integral x, net wins y are the same compared to the goal number Z. Just now we have the object array in the collection, in fact, the following is very simple, using collections.sort and Comparator can be resolved:


		Collections.sort (b, new comparator<info> () {public
			int compare (info team1, info team2) {
				int x = Team1.getx ()-Team2.getx ();
				int y = team1.gety ()-team2.gety ();
				int z = Team1.getz ()-Team2.getz ();
				if (x = = 0) { 
					if (y = = 0) {return 
						z; 
					}
					return y;
				}
				return x;
			}
		});

Let us now consider a more complex one, assuming that we change the provision to read: First, compare the integral x, Points x high ranking in the front, if the integral x is the same comparison net wins Y, net wins the ball y many position in front, if the integral x, the net wins the ball y all is same compares the goal number Z, the goal number many people rank in front. In other words, we need to talk about these multiple attribute multiple conditions in descending order, how should the above code be modified. Think again for reference:

		Collections.sort (b, new comparator<info> () {public
			int compare (info team1, info team2) {
				int x = Team1.getx ()-Team2.getx ();
				int y = team1.gety ()-team2.gety ();
				int z = Team1.getz ()-Team2.getz ();
				if (x!= 0) {return
					x > 0 -1:1;
				}
					if (y!= 0) {return
						y > 0 -1:1;
					}
					Return z > 0? -1:1;
			}
		});

To extend a little more, many topics will appear when the previous attribute x,y,z are all the same, according to the team name dictionary order. In fact, this is not difficult, on the basis of the above add a few lines can be:

		Collections.sort (b, new comparator<info> () {public
			int compare (info team1, info team2) {
				int x = team1.ge Tx ()-Team2.getx ();
				int y = team1.gety ()-team2.gety ();
				int z = Team1.getz ()-Team2.getz ();
				if (x!= 0) {return
					x > 0 -1:1;
				}
					if (y!= 0) {return
						y > 0 -1:1;
					}
					if (z!=0) {return
					z > 0 -1:1;
					}
					Return Team1.name.compareTo (Team2.name);
			}
		);

Finally, let's assume that this only requires the name of the team after the output is sorted:

		for (Info result:b) {
			System.out.println (Result.getname ());
		}

The whole idea was done.

Also, we need to mention that our main function is static, but the class info we defined belongs to the internal dynamic class, which leads to errors when debugging, so we can define the class as static when perfecting the program. Finally tidy up the whole idea is this: (according to the second descending output mentioned)

Import java.util.*;
		public class Main {static class Info {private String name;
		private int x;
		private int y;

		
		private int z;
		Public String GetName () {return name;
		public void SetName (String name) {this.name = name;
		public int Getx () {return x;
		public void setx (int x) {this.x = x;
		public int gety () {return y;
		The public void sety (int y) {this.y = y;
		public int Getz () {return z;
		public void Setz (int z) {this.z = Z;
		
		@SuppressWarnings ("unchecked") public static void main (string[] args) {Scanner sc = new Scanner (system.in);
		int N = Sc.nextint ();
		String temp = Sc.nextline ();
		Info[] A = new info[n+1];
		
		List<info> B = new ArrayList ();
			for (int i = 0; i < N; i++) {String str = sc.nextline ();
			String[] s = Str.split ("");
			A[i] = new Info ();
			A[i].setname (S[0]);
			A[i].setx (Integer.parseint (s[1));
			A[i].sety (Integer.parseint (s[2)); A[i].setz (integeR.parseint (s[3]));
		B.add (A[i]); Collections.sort (b, new comparator<info> () {public int compare (info team1, info team2) {int x = t
				Eam1.getx ()-Team2.getx ();
				int y = team1.gety ()-team2.gety ();
				int z = Team1.getz ()-Team2.getz ();
				if (x!= 0) {return x > 0 -1:1;
					} if (y!= 0) {return y > 0? -1:1;
					} if (z!=0) {return z > 0? -1:1;
			Return Team1.name.compareTo (Team2.name);

		}
		});
		for (Info result:b) {System.out.println (Result.getname ()); }
	}
}

Now let's assume that our input data is like this:


The final results of the above program are as follows:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.