Slide 4.5: Programming with the RMS (cont.) Slide 4.7: RecordMIDlet.java Home |
compare
method is called by RecordEnumeration to sort and return records in an application specified order.
The return value indicates the ordering of the two records.
The following code compares two strings that are retrieved from two records.
The three constants are defined in the RecordComparator interface:public someClass implements RecordComparator { public int compare( byte[ ] record1, byte[] record2 ) { ByteArrayInputStream bais1 = new ByteArrayInputStream( record1 ); DataInputStream dis1 = new DataInputStream( bais1 ); ByteArrayInputStream bais2 = new ByteArrayInputStream( record2 ); DataInputStream dis2 = new DataInputStream( bais2 ); String name1 = dis1.readUTF( ); String name2 = dis.readUTF( ); int num = name1.compareTo( name2 ); if ( num > 0 ) { return RecordComparator.FOLLOWS; } else if ( num < 0 ) { return recordcomparator.PRECEDES; } else { return recordcomparator.EQUIVALENT; } } }