In the previous example, we have discussed how to sort the list of objects on the basis of a single field using the Comparable and Comparator interface But, what if we have a requirement to sort ArrayList objects in accordance with more than one field like firstly, sort according to the student name and secondly, sort according to student age.

Method 2: Using comparator interface- Comparator interface is used to order the objects of a user-defined class. This interface is present in java.util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members. For instance, it may be on roll no, name, age, or anything else.

Why does a red gel appear red? Good questions - I guess it also reflects some red light and when it is back light, only red light passes through it.

A comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of the same class. Following function compare obj1 with obj2.

The red gel appears red since it is absorbing other wavelengths in the visible spectrum (except red), where the red wavelength is scattered and reflected back to the viewer's eye. The green gel is the same way. The blue gel is absorbing all but the high energy wavelengths (blue-ultraviolet), thus neither the red nor the green laser will pass through.

By changing the return value inside the compare method, you can sort in any order that you wish to, for example: For descending order just change the positions of ‘a’ and ‘b’ in the above compare method.

The Comparator interface is essential for custom sorting in Java. Understanding its proper implementation can help you write cleaner and more efficient code. The Java Programming Course provides detailed lessons on using Comparator in real-world applications.

So a red gel passes red light, easy. But why does a red gel appear red? Is that all from light passing through it? What is the surface color?

On the molecular level, do you happen to know what atoms or bonds are responsible for the absorption in these materials? Do they re-radiate and/or reflect? Is all the re-radiation infra/heat, or are there say, blue filters which radiate red when illuminated with green light?

So you have seen these color filters (or gels as they are also called). When you look through a red filter, everything looks red. What do they do to the light? I am not going to tell you the answer. However, I will show you some examples so that you can figure out the answer yourself.

If the gel doesn't absorb the photons, then the light passes through. The red gel allows the reddish wavelengths through, so that's all get we can see through it. Green light is different enough in length that it can't pass through.

It's been a while since I took physics, but from what I recall... those gels each absorb certain wavelengths of light. Sometimes that can be a very narrow band (such as with the red and green gels) or it could be a wider range of wavelengths (magenta).

When I read the above-the-cut paragraph, I assumed you were going to do something hand-wavey which relied on approximately unimodal filters. But good use of lasers.

As for how the gel can be so selective... I'm not sure, but I'd be interested in a more detailed description as to what's going on at the atomic level!

ScienceBlogs is where scientists communicate directly with the public. We are part of Science 2.0, a science education nonprofit operating under Section 501(c)(3) of the Internal Revenue Code. Please make a tax-deductible donation if you value independent science communication, collaboration, participation, and open access.

Method 1: One obvious approach is to write our own sort() function using one of the standard algorithms. This solution requires rewriting the whole sorting code for different criteria like Roll No. and Name.

I'll take a shot at answering these. A red gel appears red only because light passes through it. If you were to place one on a surface where no light passed through, such as a black piece of paper, it would just appear black.

When the red light passes through the green filter no light comes out i see. but what im wondering is why? why does o light come out?

The gel is probably scattering a portion of the laser light and absorbing the rest of it, returning to ground state non-radiatively. Cool demo, I wish I had this when TAing Gen Chem.

In this video, I am going to use a red and a green laser pointer. The nice thing about laser pointers is that they essentially create only one color of light.

© 2006-2024 Science 2.0. All rights reserved. Privacy statement. ScienceBlogs is a registered trademark of Science 2.0, a science media nonprofit operating under Section 501(c)(3) of the Internal Revenue Code. Contributions are fully tax-deductible.

Suppose we have an Array/ArrayList of our own class type, containing fields like roll no, name, address, DOB, etc, and we need to sort the array based on Roll no or name?

Internally the Sort method does call Compare method of the classes it is sorting. To compare two elements, it asks “Which is greater?” Compare method returns -1, 0, or 1 to say if it is less than, equal, or greater to the other. It uses this result to then determine if they should be swapped for their sort.