I am having trouble implementing a comparator into my program.
This program was originally written as an example of how to create a menu using swing. The program simply displays a menu, sub menu and a list of movies with their title, date, and studio. A larger array stores each movie, in its own array, with its title, date and studio.
Now, I have to find a way to sort these strings using a comparator. Something like this:
Arrays.sort(data, new Comparator<String[]>() {
public int compare(final String[] entry1, final String[] entry2) {
final String field1 = entry1[0];
final String field22 = entry2[0];
return field1.compareTo(field2);
}
});
Problem is, I have never used a comparator, nor am I sure on how to implement it.
I have added the source code for the original program below.
Any and all help will be greatly appreciated.
Thank you.
Edit This program is similar to a program from a Java textbook, but we are specifically told not to solve the sort in the fashion the textbook does.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class MenuExample extends Frame implements ActionListener
{
MenuBar mbar;
Menu menu,submenu;
MenuItem m1,m2,m3,m4,m5;
public MenuExample ()
{
String[][] data = new String[][]
{
new String[] { "Casablanca", "Warner Brothers", "1942" },
new String[] { "Citizen Kane", "RKO Pictures", "1941" },
new String[] { "Singin' in the Rain", "MGM", "1952" },
new String[] { "The Wizard of OZ", "MGM", "1930"}
};
// Set frame properties
setTitle("AWT Menu"); // Set the title
setSize(800,500); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame
// Create the menu bar
mbar=new MenuBar();
// Create the menu
menu=new Menu("Menu");
// Create the submenu
submenu=new Menu("Sub Menu");
// Create MenuItems
m1=new MenuItem("Menu Item 1");
m2=new MenuItem("Menu Item 2");
m3=new MenuItem("Menu Item 3");
m4=new MenuItem("Menu Item 4");
m5=new MenuItem("Menu Item 5");
m1.addActionListener(this);
// Attach menu items to menu
menu.add(m1);
menu.add(m2);
menu.add(m3);
// Attach menu items to submenu
submenu.add(m4);
submenu.add(m5);
// Attach submenu to menu
menu.add(submenu);
// Attach menu to menu bar
mbar.add(menu);
// Set menu bar to the frame
setMenuBar(mbar);
JPanel textPanel = new JPanel();
JTextPane textArea = new JTextPane();
Dimension textAreaDimensions = new Dimension(100, 300);
textArea.setPreferredSize(textAreaDimensions);
//width: 770 height: 1000
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Document doc=textArea.getDocument();
try
{
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
doc.insertString(doc.getLength(),data[i][j]+"\t",
textArea.getStyle("bold"));
doc.insertString(doc.getLength(),"\n",
textArea.getStyle("bold"));
}//end for
}//end try
catch (Exception e) {}
textPanel.add(scroll);
add(textPanel);
validate();
repaint();
}//end MenuExample
public static void main(String args[])
{
new MenuExample();
}//end main
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}//end actionPerformed
}//end class