我建议这样一个抽象的形状基类:
public abstract class Shape implements Comparable<Shape> {
private String color;
public Shape(String color) {
super();
this.color = color;
}
// geters & setters
@Override
public int compareTo(Shape other) {
if ((getAsciiSum(this.color)).compareTo(getAsciiSum(other.color)) < 0) {
return -1;
}
if ((getAsciiSum(this.color)).compareTo(getAsciiSum(other.color)) == 0) {
return 0;
}
return 1;
}
// toString
public int getAsciiSum() {
return getAsciiSum(this.color);
}
private Integer getAsciiSum(String str) {
int sum = 0;
for (Character ch : str.toCharArray()) {
int asciiValue = ch;
sum += asciiValue;
}
return sum;
}
// finish
和形状扩展器如下
圈子:
public class Circle extends Shape implements ShapeType {
private int radius;
public Circle(String color) {
super(color);
}
public Circle(String color, int radius) {
this(color);
this.radius = radius;
}
// geters & setters, toString
矩形:
public class Rectangle extends Shape implements ShapeType {
int width;
int highth;
public Rectangle(String color) {
super(color);
}
public Rectangle(String color, int width, int highth) {
this(color);
this.width = width;
this.highth = highth;
}
// geters & setters, toString
三角形:
public class Triangle extends Shape implements ShapeType {
private int sideOne;
private int sideTwo;
private int sideThree;
public Triangle(String color) {
super(color);
}
public Triangle(String color, int sideOne, int sideTwo, int sideThree) {
this(color);
this.sideOne = sideOne;
this.sideTwo = sideTwo;
this.sideThree = sideThree;
}
// geters & setters, toString
在以下命令之后:
System.out.println("\nbefore sort:");
print(arr3);
Arrays.sort(arr3, Shape::compareTo);
System.out.println("\nafter sort:");
print(arr3);
我们可以得到结果-
排序前:
Circle [AsciiSum=392, color=BROWN, radius=5]|i=0
Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=1
Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=2
Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=3
Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=4
Circle [AsciiSum=296, color=BLUE, radius=4]|i=5
Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=6
Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=7
Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=8
Triangle [AsciiSum=1147, color=CHILD'S SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=9
排序后:
Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=0
Circle [AsciiSum=296, color=BLUE, radius=4]|i=1
Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=2
Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=3
Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=4
Circle [AsciiSum=392, color=BROWN, radius=5]|i=5
Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=6
Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=7
Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=8
Triangle [AsciiSum=1147, color=CHILD'S SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=9
-- 编辑--
当然可以按字典顺序比较颜色字段中的字符串,然后 compareTo 方法可以很简单,如:
@Override
public int compareTo(Shape other) {
if ((this.color).compareTo(other.color) < 0) {
return -1;
}
if ((this.color).compareTo(other.color) == 0) {
return 0;
}
return 1;
}
排序后的数组元素顺序如下:
Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=0
Circle [AsciiSum=296, color=BLUE, radius=4]|i=1
Circle [AsciiSum=392, color=BROWN, radius=5]|i=2
Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=3
Triangle [AsciiSum=1147, color=CHILD'S SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=4
Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=5
Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=6
Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=7
Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=8
Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=9