简体   繁体   中英

How can I interpret the sorting in this java code?

I would really like to get some help interpreting this bit of code. How has the writer of the code used sorting? The result is a reverse numeric sort.

private void listbids() {
    List<Bid> bidl;

    String dogName = readString("Dog name:");
    Auction bidAuction = null;
    for (Auction auctions : auctionsList) {
        if (auctions.getDogName().toLowerCase().equals(dogName.toLowerCase())) {
            bidAuction = auctions;
            break;
        }
    }
    if (bidAuction != null) {
        bidl = bidAuction.getBidding();
        bidl.sort(Comparator.comparing(bid -> String.format("%08d", 999999999-bid.getAmount())));
        System.out.println("Bids:");
        for (Bid bid : bidl) {
            System.out.println("Amount: " + bid.getAmount() + " Bidder: " + bid.getUser().getName());
        }
    } else {
        System.out.println("Error: No bids registred yet for this auction");
    }
}

That's a very bizarre way of comparing numbers to say the least. The core of it is here:

String.format("%08d", 999999999-bid.getAmount())

The comparison happens by:

  1. Comparing items lexicographically rather than numerically ( String.format )
  2. Using String s representing integer-only values (the d format bit)
  3. Padding those integer values with 0 s (the 0 format bit)...
  4. ... to form 8-digit integers (the 8 format bit)
  5. After substracting the original value to 999999999 (in the assumption that the bid.getAmount() value will always be less than that)

This could probably be refactored with Comparator.comparingInt(your ToIntFunction here converting whatever bid.getAmount() returns).reversed() .

Everything is done in this line:

bidl.sort(Comparator.comparing(bid -> String.format("%08d", 999999999-bid.getAmount())));

This is a very odd way of doing a reverse numeric sort, but it should work for numbers that aren't too big (as long as there are no integer overflow). Basically, numbers are negated (which effectively reverses the ordering), added a big number (which is useless and can cause overflow) and converted to string (which is also useless).

A more conventional way would be to do:

bidl.sort(Comparator.comparing(Bid::getAmount).reversed ());

To give you a similar but very simple example to clarify what is going on in your code:

    List<Integer> list = Arrays.asList(1,3,5,6,2,7,9,8,4);
    list.sort(Comparator.comparing(i -> 10-i));

The sorting is in this line:

bidl.sort(Comparator.comparing(bid -> String.format("%08d", 999999999-bid.getAmount())));

It subtracts the value of each bid from a very high number (999999999), then with the Comparator.comparing it goes through every element of the list and compares the value of them, then sorts them.

The result is in reversed order, because he subtracts the bid from the very high number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM