From: Sébastien Guimmara Date: Fri, 08 May 2015 21:20:04 GMT Subject: Re: [PATCH 3/3] git help: group common commands by theme Message-ID: <554D2884.9040607@gmail.com> In-Reply-To: On 05/08/2015 11:17 PM, Stefan Beller wrote: > On Fri, May 8, 2015 at 2:08 PM, Sébastien Guimmara > wrote: >> On 05/06/2015 05:16 AM, Eric Sunshine wrote: >>> >>> On Mon, May 4, 2015 at 4:28 PM, Sébastien Guimmara >>>> >>>> + >>>> + if (group1 == group2) >>>> + return 0; >>>> + if (group1 > group2) >>>> + return 1; >>>> + else >>>> + return -1; >>> >>> >>> Do you also want to sort the commands alphabetically within group? >>> That is, something like this? >>> >>> struct cmdname_help *e1 = elem1; >>> struct cmdname_help *e2 = elem2; >>> >>> if (e1->group < e2->group) >>> return -1; >>> if (e1->group > e2->group) >>> return 1; >>> return strcmp(e1->name, e2->name); >>> >>>> +} >> >> >> Your version raises: >> >> help.c: In function ‘cmd_group_cmp’: >> help.c:223:28: warning: initialization discards ‘const’ qualifier from >> pointer target type [enabled by default] >> struct cmdname_help *e1 = elem1; >> ^ >> help.c:224:28: warning: initialization discards ‘const’ qualifier from >> pointer target type [enabled by default] >> struct cmdname_help *e2 = elem2; >> ^ >> >> With the cast: >> >> struct cmdname_help *e1 = (struct cmdname_help*)elem1; >> >> It compiles without a warning (gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2) >> >> > > I'd rather change the type of struct cmdname_help to be const, such > that it reads: > > const struct cmdname_help *e1 = elem1; > const struct cmdname_help *e2 = elem2; > > if (e1->group < e2->group) > return -1; > if (e1->group > e2->group) > return 1; > return strcmp(e1->name, e2->name); > > instead of casting if possible. > yes, much better, thanks.