java.util.List.sort()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(246)

本文整理了Java中java.util.List.sort()方法的一些代码示例,展示了List.sort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。List.sort()方法的具体详情如下:
包路径:java.util.List
类名称:List
方法名:sort

List.sort介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Sort the given List with a default OrderComparator.
 * <p>Optimized to skip sorting for lists with size 0 or 1,
 * in order to avoid unnecessary array extraction.
 * @param list the List to sort
 * @see java.util.List#sort(java.util.Comparator)
 */
public static void sort(List<?> list) {
  if (list.size() > 1) {
    list.sort(INSTANCE);
  }
}

代码示例来源:origin: GoogleContainerTools/jib

private List<TarArchiveEntry> getSortedEntries() {
  List<TarArchiveEntry> sortedEntries = new ArrayList<>(entries);
  sortedEntries.sort(Comparator.comparing(TarArchiveEntry::getName));
  return sortedEntries;
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Obtain the closest match from the given exception types for the given target exception.
 * @param exceptionTypes the collection of exception types
 * @param targetException the target exception to find a match for
 * @return the closest matching exception type from the given collection
 */
public static Class<? extends Throwable> findClosestMatch(
    Collection<Class<? extends Throwable>> exceptionTypes, Throwable targetException) {
  Assert.notEmpty(exceptionTypes, "Exception types must not be empty");
  if (exceptionTypes.size() == 1) {
    return exceptionTypes.iterator().next();
  }
  List<Class<? extends Throwable>> handledExceptions = new ArrayList<>(exceptionTypes);
  handledExceptions.sort(new ExceptionDepthComparator(targetException));
  return handledExceptions.get(0);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the {@link Method} mapped to the given exception type, or {@code null} if none.
 */
@Nullable
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
  List<Class<? extends Throwable>> matches = new ArrayList<>();
  for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {
    if (mappedException.isAssignableFrom(exceptionType)) {
      matches.add(mappedException);
    }
  }
  if (!matches.isEmpty()) {
    matches.sort(new ExceptionDepthComparator(exceptionType));
    return this.mappedMethods.get(matches.get(0));
  }
  else {
    return null;
  }
}

代码示例来源:origin: AxonFramework/AxonFramework

private static HandlerEnhancerDefinition[] flatten(List<HandlerEnhancerDefinition> factories) {
  List<HandlerEnhancerDefinition> flattened = new ArrayList<>(factories.size());
  for (HandlerEnhancerDefinition handlerEnhancer : factories) {
    if (handlerEnhancer instanceof MultiHandlerEnhancerDefinition) {
      flattened.addAll(((MultiHandlerEnhancerDefinition) handlerEnhancer).getDelegates());
    } else {
      flattened.add(handlerEnhancer);
    }
  }
  flattened.sort(PriorityAnnotationComparator.getInstance());
  return flattened.toArray(new HandlerEnhancerDefinition[flattened.size()]);
}

代码示例来源:origin: blynkkk/blynk-server

@SuppressWarnings("unchecked")
public static List<?> sortStringAsInt(List<?> list, String field, String order) {
  if (list.size() == 0) {
    return list;
  }
  Comparator c = new GenericStringAsIntComparator(list.get(0).getClass(), field);
  list.sort("asc".equalsIgnoreCase(order) ? c : Collections.reverseOrder(c));
  return list;
}

代码示例来源:origin: spring-projects/spring-framework

private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) {
  List<Method> matches = new ArrayList<>();
  for (MethodDescriptor methodDescriptor : methodDescriptors) {
    Method method = methodDescriptor.getMethod();
    if (isCandidateWriteMethod(method)) {
      matches.add(method);
    }
  }
  // Sort non-void returning write methods to guard against the ill effects of
  // non-deterministic sorting of methods returned from Class#getDeclaredMethods
  // under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
  matches.sort((m1, m2) -> m2.toString().compareTo(m1.toString()));
  return matches;
}

代码示例来源:origin: line/armeria

/**
 * Attaches a given {@code value} to the value list. If the list is not empty
 * the {@code value} is added, and sorted by the given {@link Comparator}.
 */
private void addValue(V value, @Nullable Comparator<V> comparator) {
  if (values == null) {
    values = new ArrayList<>();
  }
  values.add(value);
  // Sort the values using the given comparator.
  if (comparator != null && values.size() > 1) {
    values.sort(comparator);
  }
}

代码示例来源:origin: robolectric/robolectric

@Nonnull
private <T> List<Class<? extends T>> prioritize(Iterable<Class<? extends T>> iterable) {
 List<Class<? extends T>> serviceClasses = new ArrayList<>();
 for (Class<? extends T> serviceClass : iterable) {
  serviceClasses.add(serviceClass);
 }
 Comparator<Class<? extends T>> c = reverseOrder(comparing(PluginFinder::priority));
 c = c.thenComparing(Class::getName);
 serviceClasses.sort(c);
 return serviceClasses;
}

代码示例来源:origin: spring-projects/spring-framework

for (String pattern : this.patterns) {
  if (pattern.equals(destination) || this.pathMatcher.match(pattern, destination)) {
    matches.add(pattern);
if (matches.isEmpty()) {
  return null;
matches.sort(this.pathMatcher.getPatternComparator(destination));
return new DestinationPatternsMessageCondition(matches, this.pathMatcher);

代码示例来源:origin: skylot/jadx

public List<JavaPackage> getPackages() {
  List<JavaClass> classList = getClasses();
  if (classList.isEmpty()) {
    return Collections.emptyList();
  }
  Map<String, List<JavaClass>> map = new HashMap<>();
  for (JavaClass javaClass : classList) {
    String pkg = javaClass.getPackage();
    List<JavaClass> clsList = map.computeIfAbsent(pkg, k -> new ArrayList<>());
    clsList.add(javaClass);
  }
  List<JavaPackage> packages = new ArrayList<>(map.size());
  for (Map.Entry<String, List<JavaClass>> entry : map.entrySet()) {
    packages.add(new JavaPackage(entry.getKey(), entry.getValue()));
  }
  Collections.sort(packages);
  for (JavaPackage pkg : packages) {
    pkg.getClasses().sort(Comparator.comparing(JavaClass::getName));
  }
  return Collections.unmodifiableList(packages);
}

代码示例来源:origin: stanfordnlp/CoreNLP

private static final List<List<ArgumentSequence>> getFullConjunctArgumentsHelper(SemanticGraph sg, IndexedWord conjGov, IndexedWord orphanGov) {
  List<List<ArgumentSequence>> arguments = new LinkedList<>();
  for (SemanticGraphEdge edge : sg.outgoingEdgeIterable(conjGov)) {
    if (isArgument(sg, edge) && edge.getDependent().pseudoPosition() < orphanGov.pseudoPosition()) {
      List<ArgumentSequence> argumentVariants = new LinkedList<>();
      ArgumentSequence seq = new ArgumentSequence(edge.getDependent(), sg.yield(edge.getDependent()));
      argumentVariants.add(seq);
      getArgumentSubsequences(sg, edge.getDependent(), argumentVariants);
      arguments.add(argumentVariants);
    }
  }
  arguments.sort((arg1, arg2) -> arg1.get(0).head.index() - arg2.get(0).head.index());
  return arguments;
}

代码示例来源:origin: graphhopper/graphhopper

private void parseSolutionsAndAddToResponse(List<List<Label.Transition>> solutions, PointList waypoints) {
  for (List<Label.Transition> solution : solutions) {
    final List<Trip.Leg> legs = tripFromLabel.getTrip(translation, graphExplorer, accessEgressWeighting, solution);
    final PathWrapper pathWrapper = tripFromLabel.createPathWrapper(translation, waypoints, legs);
    pathWrapper.setImpossible(solution.stream().anyMatch(t -> t.label.impossible));
    pathWrapper.setTime((solution.get(solution.size()-1).label.currentTime - solution.get(0).label.currentTime));
    response.add(pathWrapper);
  }
  Comparator<PathWrapper> c = Comparator.comparingInt(p -> (p.isImpossible() ? 1 : 0));
  Comparator<PathWrapper> d = Comparator.comparingDouble(PathWrapper::getTime);
  response.getAll().sort(c.thenComparing(d));
}

代码示例来源:origin: hs-web/hsweb-framework

@Override
  public List<ActivityImpl> getUserTasksByProcDefKey(String procDefKey) {
    ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(procDefKey).orderByProcessDefinitionVersion().desc().list().get(0);
    String procDefId = definition.getId();
    List<ActivityImpl> activities = findActivities(procDefId, activity -> "userTask".equals(activity.getProperty("type")));
//
    if (null != activities) {
      activities.sort(Comparator.comparing(ProcessElementImpl::getId));
    }
    return activities;
  }

代码示例来源:origin: line/armeria

private static ImmutableList<Tag> sort(List<Tag> tags) {
  if (tags.isEmpty()) {
    return ImmutableList.of();
  }
  tags.sort(comparing(Tag::getKey));
  return ImmutableList.copyOf(tags);
}

代码示例来源:origin: oblac/jodd

/**
 * Collects all interceptors.
 */
protected void collectActionInterceptors() {
  final Collection<? extends ActionInterceptor> interceptorValues = interceptorsManager.getAllInterceptors();
  interceptors = new ArrayList<>();
  interceptors.addAll(interceptorValues);
  interceptors.sort(Comparator.comparing(a -> a.getClass().getSimpleName()));
}

代码示例来源:origin: spring-projects/spring-framework

public void stop() {
  if (this.members.isEmpty()) {
    return;
    logger.debug("Stopping beans in phase " + this.phase);
  this.members.sort(Collections.reverseOrder());
  CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
  Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the {@link Method} mapped to the given exception type, or {@code null} if none.
 */
@Nullable
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
  List<Class<? extends Throwable>> matches = new ArrayList<>();
  for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {
    if (mappedException.isAssignableFrom(exceptionType)) {
      matches.add(mappedException);
    }
  }
  if (!matches.isEmpty()) {
    matches.sort(new ExceptionDepthComparator(exceptionType));
    return this.mappedMethods.get(matches.get(0));
  }
  else {
    return null;
  }
}

代码示例来源:origin: AxonFramework/AxonFramework

private static ParameterResolverFactory[] flatten(List<ParameterResolverFactory> factories) {
  List<ParameterResolverFactory> flattened = new ArrayList<>(factories.size());
  for (ParameterResolverFactory parameterResolverFactory : factories) {
    if (parameterResolverFactory instanceof MultiParameterResolverFactory) {
      flattened.addAll(((MultiParameterResolverFactory) parameterResolverFactory).getDelegates());
    } else {
      flattened.add(parameterResolverFactory);
    }
  }
  flattened.sort(PriorityAnnotationComparator.getInstance());
  return flattened.toArray(new ParameterResolverFactory[0]);
}

代码示例来源:origin: blynkkk/blynk-server

@SuppressWarnings("unchecked")
public static List<?> sort(List<?> list, String field, String order) {
  if (list.size() == 0) {
    return list;
  }
  Comparator c = new GenericComparator(list.get(0).getClass(), field);
  list.sort("asc".equalsIgnoreCase(order) ? c : Collections.reverseOrder(c));
  return list;
}

相关文章