• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.views.trace.util;
17 
18 import java.util.Comparator;
19 
20 /**
21  * ComparatorUtils
22  *
23  * @since 2021/5/27 12:29
24  */
25 public class ComparatorUtils {
26     /**
27      * generate Comparator Ruler
28      *
29      * @param filter filter the row when row value equals filter
30      * @return Comparator
31      */
generateComparator(String filter)32     public static Comparator<Object> generateComparator(String filter) {
33         return (str1, str2) -> {
34             if (filter == null) {
35                 return 0;
36             }
37             if (filter.equals(String.valueOf(str1)) || filter.equals(String.valueOf(str2))) {
38                 return 0;
39             }
40             try {
41                 Double d1 = Double.parseDouble(String.valueOf(str1));
42                 Double d2 = Double.parseDouble(String.valueOf(str2));
43                 int val = 0;
44                 if (d1 < d2) {
45                     val = -1;
46                 } else if (d1 == d2) {
47                     val = 0;
48                 } else {
49                     val = 1;
50                 }
51                 return val;
52             } catch (NumberFormatException exception) {
53                 String s1 = String.valueOf(str1);
54                 String s2 = String.valueOf(str2);
55                 return s1.compareTo(s2);
56             }
57         };
58     }
59 }
60