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

  • SEARCH
  • Home
  • 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 8039169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:29:09+00:00 2026-06-05T03:29:09+00:00

I have code that sorts an ArrayList of elements based on one attribute called

  • 0

I have code that sorts an ArrayList of elements based on one attribute called ‘title’ which is of type String. The code uses Collator like this:

Collator( Collator collator = Collator.getInstance(); ).

I have two objects with title “@a” and the other object has title “#a”

I pass these objects as a List and call

Collections.sort(list,comparator)

This gives the order as

"@a" "#a"

Why is “#a” appearing last even though its ASCII value is less than “@a” ?

  • 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-06-05T03:29:11+00:00Added an answer on June 5, 2026 at 3:29 am

    Why is # appearing last even though its ASCII value is less than @ ?

    My clean-room implementation:

    final List<String> list = Arrays.asList("@a", "#a");
    Collections.sort(list);
    System.out.println(list);
    

    Output:

    [#a, @a]

    This code doesn’t reproduce your problem.

    For reference:
    ‘#’ is 0x23
    ‘@’ is 0x40

    Everything looks normal.


    EDIT: new code following your comment “The code uses Collator but its used as Collator collator = Collator.getInstance(); not specific to any locale.”:

    final List<String> list = Arrays.asList("@a", "#a");
    final Collator c = Collator.getInstance();
    
    Collections.sort(list, c);
    System.out.println(list);
    

    Output:

    [@a, #a]

    This reproduces your problem.

    If I use Collator.getInstance() to sort the ASCII table, this is the output I get:

    -, _, ,, ;, :, !, ?, /, ., `, ^, ‘, “, (, ), [, ], {, }, @, $, *, \, &, #, %, +, <, =, >, |, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F, g, G, h, H, i, I, j, J, k, K, l, L, m, M, n, N, o, O, p, P, q, Q, r, R, s, S, t, T, u, U, v, V, w, W, x, X, y, Y, z, Z

    You can see this is quite different from the ASCII collating order:

    “, #, $, %, &, ‘, (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, [, \, ], ^, _, `, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, {, |, }

    For OP’s interest, this is the code used to create this output:

    final List<String> list = new ArrayList<String>();
    final Collator col = Collator.getInstance();
    
    for (char c = '!'; c < '~'; c++)
    {
      list.add(c+"");
    }
    
    Collections.sort(list, col);
    System.out.println(list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some old code that uses qsort to sort an MFC CArray of
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I have a TreeMap that holds the following keys/values: private ArrayList<TreeMap<String,String>> mList = new
So I have the following code that sorts a core data fetch by the
I have the following Javascript code that sorts divs on my page: $(function() {
I have code that sorts the way I want. By multiple fields. Cool. But
I have the following code that sorts by ascending. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
I have the following code that sorts by ascending. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]

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.