ConversionNum.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.zkh360.api.brand.util;
  2. import com.zkh360.api.brand.dto.BrandInfoResult;
  3. import org.apache.commons.lang.StringUtils;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. public class ConversionNum {
  9. public static List<BrandInfoResult> getCollectionsResult(List<BrandInfoResult> brands){
  10. Comparator<BrandInfoResult> brandComparator = new Comparator<BrandInfoResult>() {
  11. @Override
  12. public int compare(BrandInfoResult o1, BrandInfoResult o2) {
  13. if (o1 == o2) {
  14. return 0;
  15. }
  16. String firstWord1 = o1.getFirstWord();
  17. String firstWord2 = o2.getFirstWord();
  18. String brandName1 = o1.getBrandName();
  19. String brandName2 = o2.getBrandName();
  20. int result = stringCompare(firstWord1, firstWord2);
  21. if (result == 0) {
  22. return stringCompare(brandName1, brandName2);
  23. } else {
  24. return result;
  25. }
  26. }
  27. };
  28. Collections.sort(brands, brandComparator);
  29. return brands;
  30. }
  31. public static int stringCompare(String str1, String str2) {
  32. if (str1 == str2) {
  33. return 0;
  34. }
  35. if (str1 == null && str2 != null) {
  36. return -1;
  37. }
  38. if (str1 != null && str2 == null) {
  39. return 1;
  40. }
  41. return str1.compareTo(str2);
  42. }
  43. }