Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

D Comparison Operators Overloading

import std.random; 
import std.stdio; 
import std.string; 
 
struct Box { 
   int volume;  
   int opCmp(const ref Box box) const { 
      return (volume == box.volume ? box.volume - volume: volume - box.volume); 
   }
   
   string toString() const { 
      return format("Volume:%s\n", volume); 
   } 
} 

void main() { 
   Box[] boxes; 
   int j = 10; 
   
   foreach (i; 0 .. 10) { 
      boxes ~= Box(j*j*j); 
      j = j-1; 
   } 
   
   writeln("Unsorted Array"); 
   writeln(boxes);  
   boxes.sort; 
   writeln("Sorted Array"); 
   writeln(boxes); 
   writeln(boxes[0]<boxes[1]); 
   writeln(boxes[0]>boxes[1]); 
   writeln(boxes[0]<=boxes[1]); 
   writeln(boxes[0]>=boxes[1]); 
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.