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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:19:09+00:00 2026-05-15T20:19:09+00:00

I’m currently playing with implementing various sorting algorithms in Java, mostly for fun, but

  • 0

I’m currently playing with implementing various sorting algorithms in Java, mostly for fun, but I’m struggling with how to do it ‘right’. That is, I want the user to be able to call the sorting algorithm of choice on anything that is comparable – ints, longs, Strings, booleans (actually, are these comparable in Java?), their own classes; whatever. The question is how to do this.

I was thinking of using a class to represent the sorting algorithm, and therefore store the things to be sorted inside using a generic list or whatever (List<E>). This would also allow me to use multiple constructors and thus allow the user to pass in the data in various forms – Lists, Arrays, whatever. Is this the correct way to do it? My current problem is that I don’t wish for the user to have to create a class when they want to sort something, I’d rather it was able to be called much like System.out.println or the like.

// Example:

int[] myInts = {5,4,3,2,1};

// This is what I do *not* want.
InsertionSort mySort = new InsertionSort();
int[] sortedInts = mySort.sort(myInts);

// This is more like what I want.
int[] sortedInts = Sorting.insertionSort(myInts);

I apologise with what may seem like a basic question, but I am just learning my way with programming languages. Which is a bit ridicolous for a 2nd year Computing student working at a software company for his summer job, but you’d be surprised at how little programming knowledge is required for most of my work… it’s usually more design knowledge.

EDIT:

For clarity, my three main question are:

  • Is it better to have the user create a class to do the sorting, or to have a static method in a class the user imports?
  • Is it possible to deal with both primitive data types and generic objects easily? Since I want to be able to handle any generic object that implements comparable (or likewise), this then causes problems with primitives (as they don’t implement anything 😉 ).
  • What is the best way to handle generic input – what should I check for before I try to sort them (implementing Comparable, for example)?
  • 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-15T20:19:10+00:00Added an answer on May 15, 2026 at 8:19 pm

    You could take as example the way Collections provides the binarySearch operation … And indeed, the

    int[] sortedInts = Sorting.insertionSort(myInts);
    

    is more java-way, even if I would personnally prefer

    public class Sorting {
         public static <DataType extends Comparable> Iterable<DataType> insertionSort(Iterable<DataType> data);
    }
    
    • <DataType> ensure output data is of the same type than input
    • Iterable<DataType> data input data is an iterable, to ensure maximum compatibility. Obviously, using a List would be by far simple, as it allows internal item reordering. Howeve”r, using an iterable ensure the implementor of this method will have to re-create the list in order to modify it, guaranteeing that the input list is left unchanged, and that the output list is another one.

    Since I just saw you edit your question, let me reply to it point by point (and consider chosing an answer after that, as it is easier to add new questions than to edit existing ones endlessly – unless you make your question a community wiki, like I do of this reply)

    Is it better to have the user create a class to do the sorting, or to have a static method in a class the user imports?

    To my mind, using a static method in this case is preferable, as you here have to manipulate objects you didn’t create, in a quite “basic” fashion.

    Is it possible to deal with both primitive data types and generic objects easily? Since I want to be able to handle any generic object that implements Comparable (or likewise), this then causes problems with primitives (as they don’t implement anything 😉 ).

    Have you heard about autoboxing ? It’s a feature of Java 5 which makes primary types “equivalents” of objects. That’s to say int are automatically converted into Integer, which, as you know, implement Comparable.

    What is the best way to handle generic input – what should I check for before I try to sort them (implementing Comparable, for example)?

    Notice that, due to my method declaration (the ), checking that input data implements Comparable is not done by you, but by the Jav compiler, allowing your IDE to show you mistakes.

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

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your second example is using a named function expression, which… May 17, 2026 at 2:09 am
  • Editorial Team
    Editorial Team added an answer Any reason why you're not using a simple <input type="submit">… May 17, 2026 at 2:09 am
  • Editorial Team
    Editorial Team added an answer When you define the Image.Source property in XAML with a… May 17, 2026 at 2:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

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.