-1

I have BookData, which has int id and String name

Coding is

bookData book = new bookData(1,"Ethan");
bookData book = new bookData(3,"Queen");
bookData book = new bookData(2,"BOB")

ArrayList al = new ArrayList();
al.add(book);

how can i use merge sort to sort id in this ArrayList?

I really need to get this done without using Collections class and Arrays Class

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    You can check this https://stackoverflow.com/questions/6818683/what-different-sorting-algorithms-are-available-in-java-6 – flyingfox Dec 04 '18 at 05:52
  • `bookData` need change to `BookData`,it's a very bad practice – flyingfox Dec 04 '18 at 05:54
  • 3
    The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask). – Andreas Dec 04 '18 at 05:54

1 Answers1

0

Besides using the correct syntaxes, you can perform List.sort as:

List<BookData> al = new ArrayList<>(); // initialise as you will ; renamed class name as 'BookData'
al.sort(Comparator.comparingInt(bookData::getId));

which performs an iterative merge sort on its own. From its implementation note -

This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

Naman
  • 27,789
  • 26
  • 218
  • 353