Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38535

Re: Is there a better way to group and sort objects?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Is there a better way to group and sort objects?
Date 2018-08-08 09:23 -0400
Organization A noiseless patient Spider
Message-ID <pkeqsl$vi3$1@dont-email.me> (permalink)
References <8c045ff4-98c5-4253-9136-b26b34cf8f77@googlegroups.com> <pke94a$j46$1@news.albasani.net> <pkeib2$9pv$1@dont-email.me> <pkejoq$ksf$1@news.albasani.net> <ac2eee07-9f11-486f-9cc4-a46a07121d87@googlegroups.com>

Show all headers | View raw


On 8/8/2018 8:50 AM, Eric Douglas wrote:
> On Wednesday, August 8, 2018 at 7:22:10 AM UTC-4, Martin Gregorie wrote:
>>
>> Another thought, seeing that Eric wants to use an integer key, why not
>> store the key value in an Integer, which is already Comparable, rather
>> than making the class containing the int key Comparable?
>>
>> This is effectively what I do when manipulating a TreeMap of objects with
>> String values as keys. A scan of the class definitions shows that the
>> same idea should work for a HashMap.
>>
> 
> I'm using objects for grouping and sorting.  For instance we have an object that is a customer address.  It has some 8-10 fields that make up the address (name, street, city, state, zip, country, attention..).  It has some boolean fields that define the purpose of the address (mail to, bill to, ship to).  The fields used in the example in this thread are integers for simplicity, actual fields may contain a collection of different variable types.

     A fair amount of running around, several red herrings, and no few
blind alleys might have been avoided had you described the actual
situation to start with.  As it is, we all got sidetracked by issues
that pertain only to the purely synthetic TestClass and friends, issues
that have very little to do with the actual problem.

     Food for thought ...

> I'm using toString() to pass out all values that make the address unique.  This is my map key, so if my ship to source contains an address that matches everything from a mail to source I combine those to get one object that is flagged as both ship to and mail to.  To combine the key must be the string, not the object as both objects would use unique object pointers if you use them as a key.

     (1) I suspect toString() will produce keys that may not be entirely
satisfactory, not unless a good deal of "regularizing" has already been
performed on the data.

	"123 Elm Street"
	"123 Elm St."
	"123 Elm St. Suite 2B"
	"123 Elm Street #2B"
	...

     (2) Concerning "unique object pointers," have you learned about the
equals() and hashCode() methods yet?  ;)

> I'm using compareTo() to define the sort order.  Sort order is computed by those flags then the address fields.  One user wants to see all mail to addresses first, another needs to see ship to addresses first.  I did start by putting a flag in each object to make the objects themselves comparable and use the flag to determine the order (all objects must have the same flag value), though that will look better with a separate Comparator class.

     I recall a thread some time ago that discussed having a "How should
I compare myself?" flag in each object instance, a flag that governs the
way compareTo() works.  IIRC, opinion was pretty much universal that
this would be a Very Bad Idea (even in a variant where the flag was a
static).  Stick with separate Comparators for separate sort orders.

-- 
esosman@comcast-dot-net.invalid
Eight hundred ninety-six days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 06:00 -0700
  Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 09:44 -0400
    Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 11:33 -0400
    Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 10:22 -0700
      Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:16 -0400
        Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:36 -0400
        Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 12:13 -0700
          Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 16:00 -0400
  Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 08:20 +0000
    Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 06:57 -0400
      Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:08 +0000
      Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:22 +0000
        Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 07:45 -0400
          Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 14:12 +0000
        Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 05:50 -0700
          Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 09:23 -0400
            Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 06:47 -0700
              Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 10:31 -0400
                Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 08:24 -0700
          Re: Is there a better way to group and sort objects? Robert Klemme <shortcutter@googlemail.com> - 2018-08-14 14:06 +0200
            Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:49 -0700
  Re: Is there a better way to group and sort objects? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-08-09 16:06 +0200
    Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:47 -0700

csiph-web