[CLI] Fix sorting by channel/category position (#490)

pull/492/head
Lucas LaBuff 4 years ago committed by GitHub
parent 77b7977324
commit 8b9afe45b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ using CliFx;
using CliFx.Attributes; using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord; using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities; using DiscordChatExporter.Domain.Utilities;
namespace DiscordChatExporter.Cli.Commands namespace DiscordChatExporter.Cli.Commands
@ -18,7 +19,7 @@ namespace DiscordChatExporter.Cli.Commands
{ {
var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId); var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId);
foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name)) foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}"); console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
} }
} }

@ -4,6 +4,7 @@ using CliFx;
using CliFx.Attributes; using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord.Models; using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities; using DiscordChatExporter.Domain.Utilities;
namespace DiscordChatExporter.Cli.Commands namespace DiscordChatExporter.Cli.Commands
@ -15,7 +16,7 @@ namespace DiscordChatExporter.Cli.Commands
{ {
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name)) foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}"); console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
} }
} }

@ -22,7 +22,7 @@ namespace DiscordChatExporter.Domain.Discord.Models
} }
// https://discord.com/developers/docs/resources/channel#channel-object // https://discord.com/developers/docs/resources/channel#channel-object
public partial class Channel : IHasId public partial class Channel : IHasIdAndPosition
{ {
public Snowflake Id { get; } public Snowflake Id { get; }

@ -8,7 +8,7 @@ using Tyrrrz.Extensions;
namespace DiscordChatExporter.Domain.Discord.Models namespace DiscordChatExporter.Domain.Discord.Models
{ {
public partial class ChannelCategory : IHasId public partial class ChannelCategory : IHasIdAndPosition
{ {
public Snowflake Id { get; } public Snowflake Id { get; }

@ -2,29 +2,29 @@
namespace DiscordChatExporter.Domain.Discord.Models.Common namespace DiscordChatExporter.Domain.Discord.Models.Common
{ {
public partial class ChannelPositionBasedComparer : IComparer<Channel> public partial class PositionBasedComparer : IComparer<IHasIdAndPosition>
{ {
public int Compare(Channel? x, Channel? y) public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y)
{ {
int result; int result;
if (x != null) if (x != null)
{ {
result = x.Position.CompareTo(y?.Position); result = x.Position.CompareTo(y?.Position);
} if(result == 0)
else if (y != null) {
{ result = x.Id.Value.CompareTo(y?.Id.Value);
result = -y.Position.CompareTo(x?.Position); }
} }
else else
{ {
result = 0; result = y == null ? 0 : -1;
} }
return result; return result;
} }
} }
public partial class ChannelPositionBasedComparer public partial class PositionBasedComparer
{ {
public static ChannelPositionBasedComparer Instance { get; } = new(); public static PositionBasedComparer Instance { get; } = new();
} }
} }

@ -0,0 +1,7 @@
namespace DiscordChatExporter.Domain.Discord.Models.Common
{
public interface IHasIdAndPosition : IHasId
{
int Position { get; }
}
}
Loading…
Cancel
Save