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 8008015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:00:41+00:00 2026-06-04T18:00:41+00:00

Some external function to me gives me a java.io.File instance, but I would like

  • 0

Some external function to me gives me a java.io.File instance, but I would like to change default behavior for compareTo for that instance on-the-fly. Whats the best approach?

The only thing I can think of is wrapping this File instance into a

public class FileWrapper extends File{   

    FileWrapper(File in){
        //Assign var to the global var      
    }

    @Overrides
    public compareTo(File in){ return whatever;}

}

And make all methods override File´s ones and forward the calls to the global wrapped instance pased through constructor, but it´s very ugly…

Maybe I am forgetting some other easier way…

  • 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-04T18:00:42+00:00Added an answer on June 4, 2026 at 6:00 pm

    The only reason you might want to use the compareTo method is to sort a collection.

    You can always create a Comparator and pass it into a Collections.sort call.

    Collections.sort(myList, new Comparator<File>() {
        public int compare(File file1, File file2) {
            // write your custom compare logic here.
        }
    });
    

    Even if you use a sorted-collection such as TreeSet, it already provides you with an overloaded constructor for passing in the Comparator.

    /**
     * Constructs a new, empty tree set, sorted according to the specified
     * comparator.  All elements inserted into the set must be <i>mutually
     * comparable</i> by the specified comparator: {@code comparator.compare(e1,
     * e2)} must not throw a {@code ClassCastException} for any elements
     * {@code e1} and {@code e2} in the set.  If the user attempts to add
     * an element to the set that violates this constraint, the
     * {@code add} call will throw a {@code ClassCastException}.
     *
     * @param comparator the comparator that will be used to order this set.
     *        If {@code null}, the {@linkplain Comparable natural
     *        ordering} of the elements will be used.
     */
    public TreeSet(Comparator<? super E> comparator) {
        this(new TreeMap<E,Object>(comparator));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add some external .dll libraries e.g. glut32.dll (but it's only
I need to get some external data to form an output file name in
My program uses some external programs that i included in the Files folder. In
I am trying to add some external javascript files to the magento cms but
In my java project, i have added some external jars. I want to update
I have some surface data that is generated by an external program as XYZ
Template methods as in NOT C++ templates . So, say that you would like
I am writing a program that will call a function from an external library,
I would like to take an object and remove some methods from it. i.e.
Suppose I have a function which consults some external stateful service and returns a

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.