String[] k1 = {"0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1"};
double[] k2 = {0.10, 0.2, 0.1, 0, 1.10, 1.2, 1.1, 1, 2.10, 2, 2.2, 2.1};
Arrays.sort(k1);
Arrays.sort(k2);
System.out.println(Arrays.toString(k1));
System.out.println(Arrays.toString(k2));
输出:
[0, 0.1, 0.10, 0.2, 1, 1.1, 1.10, 1.2, 2, 2.1, 2.10, 2.2]
[0.0, 0.1, 0.1, 0.2, 1.0, 1.1, 1.1, 1.2, 2.0, 2.1, 2.1, 2.2]
我想拥有什么,
[0, 0.1, 0.2, 0.10, 1, 1.1, 1.2, 1.10, 2, 2.1, 2.2, 2.10]
首先在小数之前和之后排序。像1,1.1,1.2,1.10,2,2.1等
如何为此编写比较器?
答案 0 :(得分:6)
2.1, 2.2, 2.10]
由于2.10按此顺序大于2.2,因此它看起来像版本号排序:
import java.util.Arrays;
import java.util.Comparator;
public class VersionNumberComparator implements Comparator<String> {
@Override
public int compare(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
int major1 = major(v1);
int major2 = major(v2);
if (major1 == major2) {
return minor(v1).compareTo(minor(v2));
}
return major1 > major2 ? 1 : -1;
}
private int major(String[] version) {
return Integer.parseInt(version[0]);
}
private Integer minor(String[] version) {
return version.length > 1 ? Integer.parseInt(version[1]) : 0;
}
public static void main(String[] args) {
String[] k1 = { "0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1",
"2.10", "2", "2.2", "2.1" };
Arrays.sort(k1, new VersionNumberComparator());
System.out.println(Arrays.asList(k1));
}
}
答案 1 :(得分:0)
我认为你想要的东西是不可能的。考虑这个简单的例子:
public static void main(String[] args) {
double d = 0.1;
System.out.println(d);
d = 0.10;
System.out.println(d);
d = 0.100;
System.out.println(d);
}
打印:
0.1
0.1
0.1
for java 0.1 double与0.10和0.100相同。 没有双0.1,0.10,0.100等,只有0.1
答案 2 :(得分:0)
你可以编写一个比较器,它首先将值转换为double并比较它们,如果它们等于“0.10”&amp; “0.1”你应该按长度比较它们。
答案 3 :(得分:0)
Traceback:
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/utils/decorators.py" in inner
145. return func(*args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/vagrant/Devel/chalktalk-legacy/chalktalk/chalktalk/shared/view_utils.py" in dispatch
21. return super(CheckPermissionsMixin, self).dispatch(request, *args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/django/views/generic/base.py" in get
158. context = self.get_context_data(**kwargs)
File "/vagrant/Devel/chalktalk-legacy/chalktalk/chalktalk/apps/exams_sat/views_take.py" in get_context_data
508. sections, self.data.user.id, many=True).is_valid(raise_exception=True):
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
221. raise ValidationError(self.errors)
Exception Type: ValidationError at /sat/474/208/bubble/
Exception Value: {u'non_field_errors': [u'Expected a list of items but got type "int".']}