Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by code_guru ( 6 years ago )
/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.Arrays;
import java.util.Comparator;
public class Main
{
	public static void main(String[] args) {
		Book books[] = {new Book(3,"C++"),new Book(4,"Data Structure"),new Book(1,"C++"),new Book(2,"Java"),new Book(3,"Algorithm")};
		
		//below code to sort by NAME;
		Arrays.sort(books, new Comparator<Book>() {
		    @Override
		    public int compare(Book b1, Book b2) {
		        return b1.getName().compareTo(b2.getName()); 
		    }
		    
		});
		
		for(Book book : books) {
		    System.out.println(book.getId() + " " + book.getName());
		}
	}
}

class Book{
    private int id;
    private String name;
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
    public Book() {
        
    }
    
    public Book(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

 

Revise this Paste

Your Name: Code Language: