Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 639741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:52:34+00:00 2026-05-13T20:52:34+00:00

Suppose I have a list (or Set): List<String> testList = Lists.newArrayList(assocX,srcT,destA,srcX, don’t care Y,

  • 0

Suppose I have a list (or Set):

List<String> testList = Lists.newArrayList("assocX","srcT","destA","srcX", "don't care Y", "garbage", "srcB");

I would like to get back an ImmutableList(Set) that sorts/groups terms in natural order where terms that begin with “src” are first, “assoc” second and “dest” last. If a term does not contain those then it should be removed from the resulting list.

Therefore the result here is “srcB”, “srcT”, “assocX”, “destA”.

I think I can do this with some combination of Iterables.filter or Predicates but just not seeing it. There must be a succinct way of doing it I think.

EDIT: A set in place of a list works as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:52:35+00:00Added an answer on May 13, 2026 at 8:52 pm

    As long as those three prefixes are the only things you care about, I’d suggest something like this:

        Predicate<String> filter = new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return input.startsWith("src") || input.startsWith("assoc") || input.startsWith("dest");
            }
        };
    
        Function<String, Integer> assignWeights = new Function<String, Integer>() {
            @Override
            public Integer apply(String from) {
                if (from.startsWith("src")) {
                    return 0;
                } else if (from.startsWith("assoc")) {
                    return 1;
                } else if (from.startsWith("dest")) {
                    return 2;
                } else {
                    /* Shouldn't be possible but have to do something */
                    throw new IllegalArgrumentException(from + " is not a valid argument");
                }
            }
        };
    
        ImmutableList<String> sortedFiltered = ImmutableList.copyOf(
                Ordering.natural().onResultOf(assignWeights).sortedCopy(
                        Iterables.filter(testList, filter)
                )
        );
    

    This solution definitely wouldn’t scale out incredibly well if you start adding more prefixes to filter out or sort by, since you’d have to continually update both the filter and the weight of each prefix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two lists that holds the list of source file names and
Suppose I have a function list_fun : int_list -> string list and in that
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Suppose I have a drop-down list like: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option
Suppose I have a collection (be it an array, generic List, or whatever is
i have a List<my_Custom_Class> and List<string> . My custom class has various items in
Suppose I have one class Person ……… public class Person { public string Name
Suppose you have a list of MyObject like this: public class MyObject { public
Suppose I have a List[Int] , and I want to call toString on each
I have a string that represents a list: [22, 33, 36, 41, 46, 49,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.