Skip to content

Commit 1184180

Browse files
committed
Perform a stable sort on CoapMessage.Options
Fixes #11
1 parent 8bee2ef commit 1184180

3 files changed

Lines changed: 7 additions & 12 deletions

File tree

src/CoAPNet/CoapMessage.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ public List<CoapOption> Options
166166
get { return _options; }
167167
set
168168
{
169-
_options = value;
170-
_options.Sort();
169+
_options = value.OrderBy(o => o.OptionNumber).ToList();
171170
}
172171
}
173172

@@ -244,8 +243,7 @@ public byte[] ToBytes()
244243
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
245244
var currentOptionDelta = 0;
246245

247-
_options.Sort();
248-
foreach (var option in _options)
246+
foreach (var option in _options.OrderBy(o => o.OptionNumber))
249247
{
250248
var optionHeader = new List<byte>();
251249
int optionDelta = option.OptionNumber - currentOptionDelta;

src/CoAPNet/CoapOptions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static bool Contains(this ICollection<CoapOption> collection, int optionN
132132
/// <summary>
133133
/// Repressents a CoAP-Option (Much like HTTP Headers) that may be present in a <see cref="CoapMessage"/>.
134134
/// </summary>
135-
public class CoapOption : IComparable<CoapOption>
135+
public class CoapOption
136136
{
137137
private readonly int _optionNumber;
138138
/// <summary>
@@ -471,10 +471,5 @@ public override int GetHashCode()
471471

472472
return hashcode;
473473
}
474-
475-
int IComparable<CoapOption>.CompareTo(CoapOption other)
476-
{
477-
return _optionNumber - other._optionNumber;
478-
}
479474
}
480475
}

tests/CoAPNet.Tests/CoapMessageTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,24 @@ public void TestMessageOptionsOutOfOrder()
232232
new Options.UriHost("example.net"),
233233
new Options.UriPath(".well-known"),
234234
new Options.UriPath("core"),
235+
new Options.ContentFormat(Options.ContentFormatType.ApplicationJson),
235236
new Options.Accept(Options.ContentFormatType.ApplicationLinkFormat),
236237
};
237238

238239
// Act
239240
_message = new CoapMessage
240241
{
241242
Options = new List<CoapOption> {
242-
new Options.Accept(Options.ContentFormatType.ApplicationLinkFormat),
243+
new Options.Accept(Options.ContentFormatType.ApplicationJson),
243244
new Options.UriPath(".well-known"),
244245
new Options.UriPath("core"),
245246
new Options.UriHost("example.net"),
247+
new Options.ContentFormat(Options.ContentFormatType.ApplicationJson),
246248
}
247249
};
248250

249251
// Assert
250-
Assert.IsTrue(expectedOptions.SequenceEqual(_message.Options));
252+
Assert.That(_message.Options, Is.EqualTo(expectedOptions));
251253
}
252254

253255
[Test]

0 commit comments

Comments
 (0)